package view.dlct.loaichungkhoan.backing;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

import java.sql.SQLException;
import java.sql.Timestamp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import java.util.TimeZone;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.MethodExpression;
import javax.el.ValueExpression;

import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UISelectItems;
import javax.faces.context.FacesContext;
import javax.faces.convert.DateTimeConverter;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

import oracle.adf.model.binding.DCIteratorBinding;
import oracle.adf.view.rich.component.rich.RichDialog;
import oracle.adf.view.rich.component.rich.RichPopup;
import oracle.adf.view.rich.component.rich.data.RichColumn;
import oracle.adf.view.rich.component.rich.data.RichTable;
import oracle.adf.view.rich.component.rich.input.RichInputDate;
import oracle.adf.view.rich.component.rich.input.RichInputFile;
import oracle.adf.view.rich.component.rich.input.RichInputText;
import oracle.adf.view.rich.component.rich.input.RichSelectOneChoice;
import oracle.adf.view.rich.component.rich.input.RichSelectOrderShuttle;
import oracle.adf.view.rich.component.rich.input.RichTextEditor;
import oracle.adf.view.rich.component.rich.layout.RichPanelFormLayout;
import oracle.adf.view.rich.component.rich.layout.RichPanelGroupLayout;
import oracle.adf.view.rich.component.rich.layout.RichPanelTabbed;
import oracle.adf.view.rich.component.rich.layout.RichShowDetailItem;
import oracle.adf.view.rich.component.rich.nav.RichCommandButton;
import oracle.adf.view.rich.component.rich.nav.RichCommandLink;
import oracle.adf.view.rich.component.rich.nav.RichCommandToolbarButton;
import oracle.adf.view.rich.component.rich.output.RichOutputText;
import oracle.adf.view.rich.context.AdfFacesContext;
import oracle.adf.view.rich.event.DialogEvent;
import oracle.adf.view.rich.event.PopupCanceledEvent;

import oracle.jbo.NameValuePairs;
import oracle.jbo.Row;
import oracle.jbo.RowValException;
import oracle.jbo.domain.BlobDomain;
import oracle.jbo.domain.ClobDomain;
import oracle.jbo.domain.DBSequence;
import oracle.jbo.domain.Number;

import org.apache.myfaces.trinidad.component.UIXInput;
import org.apache.myfaces.trinidad.event.SelectionEvent;
import org.apache.myfaces.trinidad.model.RowKeySet;
import org.apache.myfaces.trinidad.model.RowKeySetImpl;
import org.apache.myfaces.trinidad.model.UploadedFile;
import org.apache.myfaces.trinidad.render.ExtendedRenderKitService;
import org.apache.myfaces.trinidad.util.Service;
import org.apache.myfaces.trinidadinternal.taglib.util.MethodExpressionMethodBinding;

import view.common.Utils;
import view.common.search.AdvancedSearchGroup;
import view.common.search.AdvancedSearchItem;

public class LoaiChungKhoanBean {
    private RichPanelFormLayout searchPanel = new RichPanelFormLayout();
    private RichTable tableLoaiChungKhoan = new RichTable();
    private RichSelectOneChoice goToPage = new RichSelectOneChoice();
    private RichPopup popupUpdate = new RichPopup();
    private RichDialog dialogUpdate = new RichDialog();
    private RichPopup popupDelete = new RichPopup();
    private RichSelectOrderShuttle selectNgonNgu =
        new RichSelectOrderShuttle();
    private RichPanelTabbed updatePanel = new RichPanelTabbed();
    private final String SEARCH_ID_PREFIX = "search";
    private final String UPDATE_ID_PREFIX = "update";
    private final String ROW_KEY = "rowkey";
    private final String FILE_GROUP_ID_SUBFIX = "group";
    private final String FILE_DOWNLOAD_ID_SUBFIX = "download";
    private final String FILE_REMOVE_ID_SUBFIX = "remove";
    private final String DEFAULT_LANGCODE="vi";
    private RichCommandToolbarButton firstPage =
        new RichCommandToolbarButton();
    private RichCommandToolbarButton prevPage = new RichCommandToolbarButton();
    private RichCommandToolbarButton nextPage = new RichCommandToolbarButton();
    private RichCommandToolbarButton lastPage = new RichCommandToolbarButton();
    private RichCommandButton fileDownloadButton;

    public LoaiChungKhoanBean() {
    }

    public void beforePhase(PhaseEvent phaseEvent) {
        if (Boolean.FALSE.equals(Utils.getElExpression("#{adfFacesContext.postback}"))) {
            Utils.search("DlctLoaiChungKhoanView1Iterator", new ArrayList());
            resetForm();
            getValue(0);
            rollback(null);
        }
    }

    private void resetForm() {
        searchPanel.getChildren().clear();
        tableLoaiChungKhoan.getChildren().clear();
        List<AdvancedSearchGroup> listASG = new ArrayList();
        AdvancedSearchGroup asg = new AdvancedSearchGroup();
        listASG.add(asg);
        AdvancedSearchItem asi = new AdvancedSearchItem();
        asg.getListChildren().add(asi);
        asi.setItemName("DlctLoaiTruongTt");
        asi.setRequired(true);
        asi.getListValue().add(0,
                               new Number(Utils.ENUM_LOAI_TRUONG_TT.LOAI_CHUNG_KHOAN.getId()));
        asi = new AdvancedSearchItem();
        asg.getListChildren().add(asi);
        asi.setItemName("TrangThai");
        asi.setRequired(true);
        asi.getListValue().add(0,
                               new Number(Utils.ENUM_TRANG_THAI.CONG_BO.getId()));
        Utils.searchadvanced("DlctTruongTtView1Iterator", listASG);
        DCIteratorBinding truongTtView1Iterator =
            Utils.getIteratorBinding("DlctTruongTtView1Iterator");
        truongTtView1Iterator.setRangeSize(-1);
        Row[] rows = truongTtView1Iterator.getAllRowsInRange();
        if (rows == null) {
            rows = new Row[] { };
        }
        for (int i = 0; i < rows.length; i++) {
            String id = ((DBSequence)rows[i].getAttribute("Id")).toString();
            String ten = (String)rows[i].getAttribute("TenTruong");
            Number kieuDuLieu = (Number)rows[i].getAttribute("KieuDuLieu");
            Number viTriHienThi = (Number)rows[i].getAttribute("ViTriHienThi");
            if (viTriHienThi != null && kieuDuLieu != null) {
                int viTri = viTriHienThi.intValue();
                int kieu = kieuDuLieu.intValue();
                if ((Utils.ENUM_VI_TRI_HIEN_THI.SEARCH.getId() & viTri) ==
                    Utils.ENUM_VI_TRI_HIEN_THI.SEARCH.getId()) {
                    List<UIComponent> components =
                        createSearchComponent(SEARCH_ID_PREFIX +
                                              rows[i].getKey().toStringFormat(false),
                                              ten, kieu);
                    if (components != null) {
                        searchPanel.getChildren().addAll(components);
                    }
                }
                if ((Utils.ENUM_VI_TRI_HIEN_THI.TABLE.getId() & viTri) ==
                    Utils.ENUM_VI_TRI_HIEN_THI.TABLE.getId()) {
                    UIComponent component = createColumn(id, ten,kieu);
                    if (component != null) {
                        tableLoaiChungKhoan.getChildren().add(component);
                    }
                }
            }
        }
        if (searchPanel.getChildren().size() > 0) {
            FacesContext fc = FacesContext.getCurrentInstance();
            Application app = fc.getApplication();
            ExpressionFactory elFactory = app.getExpressionFactory();
            ELContext elContext = fc.getELContext();
            MethodExpression methodExpression =
                elFactory.createMethodExpression(elContext,
                                                 "#{loaiChungKhoanBean.timKiem}",
                                                 null,
                                                 new Class[] { ActionEvent.class });
            ValueExpression valueExp =
                elFactory.createValueExpression(elContext,
                                                "#{adfBundle['view.ViewControllerBundle'].TIM_KIEM}",
                                                Object.class);
            MethodExpressionMethodBinding methodExpressionMethodBinding =
                new MethodExpressionMethodBinding(methodExpression);
            RichCommandButton buttonSearch = new RichCommandButton();
            buttonSearch.setId("searchButton");
            buttonSearch.setValueExpression("text", valueExp);
            buttonSearch.getAttributes().put("actionListener",
                                             methodExpressionMethodBinding);
            searchPanel.getChildren().add(buttonSearch);
        }
    }

    private void getValue(int pageNum) {
        resetPaging("DlctLoaiChungKhoanView1Iterator", pageNum);
        DCIteratorBinding loaiChungKhoanView1Iterator =
            Utils.getIteratorBinding("DlctLoaiChungKhoanView1Iterator");
        List<HashMap<String, Object>> listValue = new ArrayList();
        Row[] rows = loaiChungKhoanView1Iterator.getAllRowsInRange();
        if (rows == null) {
            rows = new Row[] { };
        }
        String currentRowKey = "";
        if (loaiChungKhoanView1Iterator.getCurrentRow() != null &&
            loaiChungKhoanView1Iterator.getCurrentRowIndexInRange() >= 0 &&
            loaiChungKhoanView1Iterator.getCurrentRowIndexInRange() <
            loaiChungKhoanView1Iterator.getRangeSize()) {
            currentRowKey =
                    loaiChungKhoanView1Iterator.getCurrentRow().getKey().toStringFormat(false);
        }
        RowKeySet rowKeySet = tableLoaiChungKhoan.getSelectedRowKeys();
        if (rowKeySet == null) {
            rowKeySet = new RowKeySetImpl();
            tableLoaiChungKhoan.setSelectedRowKeys(rowKeySet);
        }
        rowKeySet.removeAll();
        for (int i = 0; i < rows.length; i++) {
            loaiChungKhoanView1Iterator.setCurrentRowIndexInRange(i);
            String rowKey =
                loaiChungKhoanView1Iterator.getCurrentRow().getKey().toStringFormat(false);
            if (currentRowKey == null || currentRowKey.isEmpty()) {
                currentRowKey = rowKey;
            }
            if (currentRowKey.equals(rowKey)) {
                rowKeySet.add(i);
            }
            HashMap<String, Object> mapValue = new HashMap();
            mapValue.put(ROW_KEY, rowKey);
            DCIteratorBinding gtLckTtView1Iterator =
                Utils.getIteratorBinding("DlctGtLckTtView1Iterator");
            gtLckTtView1Iterator.setRangeSize(-1);
            Row[] rowsGtTt = gtLckTtView1Iterator.getAllRowsInRange();
            if (rowsGtTt == null) {
                rowsGtTt = new Row[] { };
            }
            for (int j = 0; j < rowsGtTt.length; j++) {
                Row truongTtRow =
                    (Row)rowsGtTt[j].getAttribute("DlctTruongTtView");
                if (truongTtRow != null) {
                    Number id =
                        ((DBSequence)truongTtRow.getAttribute("Id")).getSequenceNumber();
                    Number kieuDuLieu =
                        (Number)truongTtRow.getAttribute("KieuDuLieu");
                    if (id != null && kieuDuLieu != null) {
                        int kieu = kieuDuLieu.intValue();
                        if (kieu == Utils.ENUM_KIEU_DU_LIEU.NUMBER.getId()) {
                            Number valNumber =
                                (Number)rowsGtTt[j].getAttribute("ValNumber");
                            if (valNumber != null) {
                                mapValue.put(id.toString(),
                                             valNumber.toString());
                            }
                        }
                        if (kieu == Utils.ENUM_KIEU_DU_LIEU.VARCHAR.getId()) {
                            String valVarchar =
                                (String)rowsGtTt[j].getAttribute("ValVarchar");
                            if (valVarchar != null) {
                                mapValue.put(id.toString(), valVarchar);
                            }
                        }
                        if (kieu == Utils.ENUM_KIEU_DU_LIEU.LINK.getId()) {
                            String valVarchar =
                                (String)rowsGtTt[j].getAttribute("ValVarchar");
                            if (valVarchar != null) {
                                mapValue.put(id.toString(), valVarchar);
                            }
                        }
                        if (kieu == Utils.ENUM_KIEU_DU_LIEU.EMAIL.getId()) {
                            String valVarchar =
                                (String)rowsGtTt[j].getAttribute("ValVarchar");
                            if (valVarchar != null) {
                                mapValue.put(id.toString(), valVarchar);
                            }
                        }
                        if (kieu == Utils.ENUM_KIEU_DU_LIEU.DATE.getId()) {
                            oracle.jbo.domain.Date valDate =
                                (oracle.jbo.domain.Date)rowsGtTt[j].getAttribute("ValDate");
                            if (valDate != null) {
                                java.util.Date date = valDate.dateValue();
                                if (date != null) {
                                    mapValue.put(id.toString(), date);
                                }
                            }
                        }
                        if (kieu == Utils.ENUM_KIEU_DU_LIEU.CLOB.getId()) {
                            ClobDomain valClob =
                                (ClobDomain)rowsGtTt[j].getAttribute("ValClob");
                            if (valClob != null) {
                                mapValue.put(id.toString(),
                                             valClob.toString());
                            }
                        }
                        if (kieu == Utils.ENUM_KIEU_DU_LIEU.FILE.getId()) {
                            Row fileViewRow =
                                (Row)rowsGtTt[j].getAttribute("HtFileView");
                            if (fileViewRow != null) {
                                String tenFile =
                                    (String)fileViewRow.getAttribute("TenFile");
                                if (tenFile != null) {
                                    mapValue.put(id.toString(), tenFile);
                                }
                            }
                        }
                    }
                }
            }
            listValue.add(mapValue);
        }
        if (currentRowKey != null && currentRowKey.isEmpty() == false) {
            loaiChungKhoanView1Iterator.setCurrentRowWithKey(currentRowKey);
        }
        tableLoaiChungKhoan.setValue(listValue);
    }

    private void resetPaging(String iteratorName, int pageNum) {
        DCIteratorBinding iterator = Utils.getIteratorBinding(iteratorName);
        int rowCount = (int)iterator.getEstimatedRowCount();
        int pageSize = Utils.getPageSize("HtCauHinhView1Iterator");
        int pageCount = 1;
        if (rowCount > pageSize) {
            if (rowCount % pageSize == 0) {
                pageCount = rowCount / pageSize;
            } else {
                pageCount = rowCount / pageSize + 1;
            }
        }

        if (pageNum < 1) {
            pageNum = 1;
        }
        if (pageNum > pageCount) {
            pageNum = pageCount;
        }
        if (pageNum == 1) {
            firstPage.setDisabled(true);
            prevPage.setDisabled(true);
        } else {
            firstPage.setDisabled(false);
            prevPage.setDisabled(false);
        }
        if (pageNum == pageCount) {
            lastPage.setDisabled(true);
            nextPage.setDisabled(true);
        } else {
            lastPage.setDisabled(false);
            nextPage.setDisabled(false);
        }
        int first = 1;
        if (pageNum < 5) {
            first = 1;
        } else if (pageNum + 9 > pageCount) {
            if (pageNum + 4 > pageCount) {
                if (pageCount - 6 < 1)
                    first = 1;
                else {
                    first = pageCount - 6;
                }
            } else {
                if (pageCount < 10) {
                    first = 1;
                } else {
                    first = pageNum - 4;
                }
            }
        } else {
            first = pageNum - 4;
        }
        List<SelectItem> listSelectItem = new ArrayList();

        for (int i = first, j = 0; i <= pageCount && j < 10; i++, j++) {
            SelectItem si = new SelectItem();
            si.setValue(""+i);
            si.setLabel(String.valueOf(i));
            listSelectItem.add(si);
        }
        UISelectItems uiSelectItems = new UISelectItems();
        uiSelectItems.setValue(listSelectItem);
        goToPage.setValue(""+pageNum);
        goToPage.getChildren().clear();
        goToPage.getChildren().add(uiSelectItems);
        goToPage.setValuePassThru(true);
        goToPage.setAutoSubmit(true);
        Row currentRow = iterator.getCurrentRow();
        iterator.executeQuery();
        if (currentRow != null) {
            iterator.setCurrentRowWithKey(currentRow.getKey().toStringFormat(false));
        }
        if ((pageNum) * pageSize > rowCount) {
            iterator.setRangeSize(rowCount - (pageNum - 1) * pageSize);
        } else {
            iterator.setRangeSize(pageSize);
        }
        iterator.setRangeStart((pageNum - 1) * pageSize);

        AdfFacesContext.getCurrentInstance().addPartialTarget(firstPage);
        AdfFacesContext.getCurrentInstance().addPartialTarget(prevPage);
        AdfFacesContext.getCurrentInstance().addPartialTarget(nextPage);
        AdfFacesContext.getCurrentInstance().addPartialTarget(lastPage);
        AdfFacesContext.getCurrentInstance().addPartialTarget(goToPage);
    }

    public void makeCurrent(SelectionEvent selectionEvent) {
        Map<String, Object> mapSelectedRow =
            (Map<String, Object>)tableLoaiChungKhoan.getSelectedRowData();
        if (mapSelectedRow != null) {
            DCIteratorBinding loaiChungKhoanView1Iterator =
                Utils.getIteratorBinding("DlctLoaiChungKhoanView1Iterator");
            loaiChungKhoanView1Iterator.setCurrentRowWithKey((String)mapSelectedRow.get(ROW_KEY));
        }
    }

    public void showPopupThemMoi(ActionEvent actionEvent) {
        Utils.search("DlctLoaiChungKhoanView1Iterator", new ArrayList());
        FacesContext fc = FacesContext.getCurrentInstance();
        Application app = fc.getApplication();
        ExpressionFactory elFactory = app.getExpressionFactory();
        ELContext elContext = fc.getELContext();

        List<AdvancedSearchGroup> listASG = new ArrayList();
        AdvancedSearchGroup asg = new AdvancedSearchGroup();
        listASG.add(asg);
        AdvancedSearchItem asi = new AdvancedSearchItem();
        asg.getListChildren().add(asi);
        asi.setItemName("TrangThai");
        asi.setRequired(true);
        asi.getListValue().add(0,
                               new Number(Utils.ENUM_TRANG_THAI.CONG_BO.getId()));
        Utils.searchadvanced("DlctNgonNguView1Iterator", listASG);

        DCIteratorBinding ngonNguView1Iterator =
            Utils.getIteratorBinding("DlctNgonNguView1Iterator");
        List<SelectItem> listSelectItem = new ArrayList();
        ngonNguView1Iterator.setRangeSize(-1);
        Row[] rows = ngonNguView1Iterator.getAllRowsInRange();
        if (rows == null) {
            rows = new Row[] { };
        }
        for (int i = 0; i < rows.length; i++) {
            String id = ((DBSequence)rows[i].getAttribute("Id")).toString();
            String ma = (String)rows[i].getAttribute("Ma");
            String ten = (String)rows[i].getAttribute("Ten");
            SelectItem si = new SelectItem();
            si.setValue(id);
            si.setLabel(ma + "_" + ten);
            listSelectItem.add(si);
        }
        UISelectItems uiSelectItems = new UISelectItems();
        uiSelectItems.setValue(listSelectItem);
        //selectNgonNgu.setLabel("ngonngu");
        selectNgonNgu.setValue(null);
        selectNgonNgu.getChildren().clear();
        selectNgonNgu.getChildren().add(uiSelectItems);
        selectNgonNgu.setValuePassThru(true);
        selectNgonNgu.setAutoSubmit(true);
        MethodExpression methodExpression =
            elFactory.createMethodExpression(elContext,
                                             "#{loaiChungKhoanBean.changeNgonNgu}",
                                             null,
                                             new Class[] { ValueChangeEvent.class });
        MethodExpressionMethodBinding methodExpressionMethodBinding =
            new MethodExpressionMethodBinding(methodExpression);
        selectNgonNgu.getAttributes().put("valueChangeListener",
                                          methodExpressionMethodBinding);
        updatePanel.getChildren().clear();
        updatePanel.setVisible(false);
        updatePanel.setChildCreation(RichPanelTabbed.CHILD_CREATION_LAZY_UNCACHED);
        dialogUpdate.setTitle((String)Utils.getElExpression("#{adfBundle['view.ViewControllerBundle'].THEM_MOI}"));
        dialogUpdate.setAffirmativeTextAndAccessKey((String)Utils.getElExpression("#{adfBundle['view.ViewControllerBundle'].THEM_MOI}"));


        DCIteratorBinding loaiChungKhoanView1Iterator =
            Utils.getIteratorBinding("DlctLoaiChungKhoanView1Iterator");
        Row row =
            loaiChungKhoanView1Iterator.getViewObject().createAndInitRow(new NameValuePairs());
        loaiChungKhoanView1Iterator.executeQuery();
        loaiChungKhoanView1Iterator.setCurrentRowWithKey(row.getKey().toStringFormat(false));
        Utils.showPopup(popupUpdate);
    }

    public void showPopupSua(ActionEvent actionEvent) {
        if (tableLoaiChungKhoan.getSelectedRowData() != null) {
            DCIteratorBinding lckNnView1Iterator =
                Utils.getIteratorBinding("DlctLckNnView1Iterator");
            DCIteratorBinding gtLckTtView1Iterator =
                Utils.getIteratorBinding("DlctGtLckTtView1Iterator");
            lckNnView1Iterator.setRangeSize(-1);
            Row[] rows = lckNnView1Iterator.getAllRowsInRange();
            if (rows == null) {
                rows = new Row[] { };
            }
            List<String> listNgonNguId = new ArrayList();
            for (int i = 0; i < rows.length; i++) {
                Number ngonNguId =
                    (Number)rows[i].getAttribute("DlctNgonNguId");
                if (ngonNguId != null) {
                    listNgonNguId.add(ngonNguId.toString());
                }
            }


            FacesContext fc = FacesContext.getCurrentInstance();
            Application app = fc.getApplication();
            ExpressionFactory elFactory = app.getExpressionFactory();
            ELContext elContext = fc.getELContext();


            List<AdvancedSearchGroup> listASG = new ArrayList();
            AdvancedSearchGroup asg = new AdvancedSearchGroup();
            listASG.add(asg);
            AdvancedSearchItem asi = new AdvancedSearchItem();
            asg.getListChildren().add(asi);
            asi.setItemName("TrangThai");
            asi.setRequired(true);
            asi.getListValue().add(0,
                                   new Number(Utils.ENUM_TRANG_THAI.CONG_BO.getId()));
            Utils.searchadvanced("DlctNgonNguView1Iterator", listASG);

            DCIteratorBinding ngonNguView1Iterator =
                Utils.getIteratorBinding("DlctNgonNguView1Iterator");
            List<SelectItem> listSelectItem = new ArrayList();
            ngonNguView1Iterator.setRangeSize(-1);
            rows = ngonNguView1Iterator.getAllRowsInRange();
            if (rows == null) {
                rows = new Row[] { };
            }
            for (int i = 0; i < rows.length; i++) {
                String id =
                    ((DBSequence)rows[i].getAttribute("Id")).toString();
                String ma = (String)rows[i].getAttribute("Ma");
                String ten = (String)rows[i].getAttribute("Ten");
                SelectItem si = new SelectItem();
                si.setValue(id);
                si.setLabel(ma + "_" + ten);
                listSelectItem.add(si);
            }
            UISelectItems uiSelectItems = new UISelectItems();
            uiSelectItems.setValue(listSelectItem);
            //selectNgonNgu.setLabel("ngonngu");
            selectNgonNgu.setValue(listNgonNguId);
            selectNgonNgu.getChildren().clear();
            selectNgonNgu.getChildren().add(uiSelectItems);
            selectNgonNgu.setValuePassThru(true);
            selectNgonNgu.setAutoSubmit(true);
            MethodExpression methodExpression =
                elFactory.createMethodExpression(elContext,
                                                 "#{loaiChungKhoanBean.changeNgonNgu}",
                                                 null,
                                                 new Class[] { ValueChangeEvent.class });
            MethodExpressionMethodBinding methodExpressionMethodBinding =
                new MethodExpressionMethodBinding(methodExpression);
            selectNgonNgu.getAttributes().put("valueChangeListener",
                                              methodExpressionMethodBinding);
            updatePanel.getChildren().clear();
            updatePanel.setVisible(true);
            updatePanel.setChildCreation(RichPanelTabbed.CHILD_CREATION_LAZY_UNCACHED);

            Map<String, RichPanelFormLayout> mapNgonNguLayout = new HashMap();
            for (String ngonNguId : listNgonNguId) {
                ngonNguView1Iterator.setCurrentRowWithKeyValue(ngonNguId);
                RichShowDetailItem showDetailItem = new RichShowDetailItem();
                showDetailItem.setImmediate(true);
                showDetailItem.setText((String)ngonNguView1Iterator.getCurrentRow().getAttribute("Ten"));
                RichPanelFormLayout formLayout = new RichPanelFormLayout();
                mapNgonNguLayout.put(ngonNguId, formLayout);
                showDetailItem.getChildren().add(formLayout);
                updatePanel.getChildren().add(showDetailItem);
            }

            Map<Number, Object> mapValue = new HashMap();
            Map<Number, String> mapGtKey = new HashMap();
            gtLckTtView1Iterator.setRangeSize(-1);
            rows = gtLckTtView1Iterator.getAllRowsInRange();
            if (rows == null) {
                rows = new Row[] { };
            }
            for (int i = 0; i < rows.length; i++) {
                Row truongTtRow =
                    (Row)rows[i].getAttribute("DlctTruongTtView");
                if (truongTtRow != null) {
                    Number id =
                        ((DBSequence)truongTtRow.getAttribute("Id")).getSequenceNumber();
                    Number kieuDuLieu =
                        (Number)truongTtRow.getAttribute("KieuDuLieu");
                    if (id != null && kieuDuLieu != null) {
                        int kieu = kieuDuLieu.intValue();
                        mapGtKey.put(id,
                                     rows[i].getKey().toStringFormat(false));
                        if (kieu == Utils.ENUM_KIEU_DU_LIEU.NUMBER.getId()) {
                            Number valNumber =
                                (Number)rows[i].getAttribute("ValNumber");
                            if (valNumber != null) {
                                mapValue.put(id, valNumber.toString());
                            }
                        }
                        if (kieu == Utils.ENUM_KIEU_DU_LIEU.VARCHAR.getId()) {
                            String valVarchar =
                                (String)rows[i].getAttribute("ValVarchar");
                            if (valVarchar != null) {
                                mapValue.put(id, valVarchar);
                            }
                        }
                        if (kieu == Utils.ENUM_KIEU_DU_LIEU.LINK.getId()) {
                            String valVarchar =
                                (String)rows[i].getAttribute("ValVarchar");
                            if (valVarchar != null) {
                                mapValue.put(id, valVarchar);
                            }
                        }
                        if (kieu == Utils.ENUM_KIEU_DU_LIEU.EMAIL.getId()) {
                            String valVarchar =
                                (String)rows[i].getAttribute("ValVarchar");
                            if (valVarchar != null) {
                                mapValue.put(id, valVarchar);
                            }
                        }
                        if (kieu == Utils.ENUM_KIEU_DU_LIEU.DATE.getId()) {
                            oracle.jbo.domain.Date valDate =
                                (oracle.jbo.domain.Date)rows[i].getAttribute("ValDate");
                            if (valDate != null) {
                                java.util.Date date = valDate.dateValue();
                                mapValue.put(id, date);
                            }
                        }
                        if (kieu == Utils.ENUM_KIEU_DU_LIEU.CLOB.getId()) {
                            ClobDomain valClob =
                                (ClobDomain)rows[i].getAttribute("ValClob");
                            if (valClob != null) {
                                mapValue.put(id, valClob.toString());
                            }
                        }
                        if (kieu == Utils.ENUM_KIEU_DU_LIEU.FILE.getId()) {
                            Row fileViewRow =
                                (Row)rows[i].getAttribute("HtFileView");
                            if (fileViewRow != null) {
                                String tenFile =
                                    (String)fileViewRow.getAttribute("TenFile");
                                if (tenFile != null) {
                                    mapValue.put(id, tenFile);
                                }
                            }
                        }
                    }
                }
            }
            searchTruongTt();
            DCIteratorBinding truongTtView1Iterator =
                Utils.getIteratorBinding("DlctTruongTtView1Iterator");
            truongTtView1Iterator.setRangeSize(-1);
            rows = truongTtView1Iterator.getAllRowsInRange();
            if (rows == null) {
                rows = new Row[] { };
            }
            for (int i = 0; i < rows.length; i++) {
                Number id =
                    ((DBSequence)rows[i].getAttribute("Id")).getSequenceNumber();
                String ten = (String)rows[i].getAttribute("TenTruong");
                Number batBuoc = (Number)rows[i].getAttribute("BatBuoc");
                Number duyNhat = (Number)rows[i].getAttribute("DuyNhat");
                Number kieuDuLieu = (Number)rows[i].getAttribute("KieuDuLieu");
                Number ngonNguId =
                    (Number)rows[i].getAttribute("DlctNgonNguId");
                if (batBuoc != null && kieuDuLieu != null &&
                    ngonNguId != null) {
                    RichPanelFormLayout formLayout =
                        mapNgonNguLayout.get(String.valueOf(ngonNguId.intValue()));
                    String gtKey = mapGtKey.get(id);
                    Object value = mapValue.get(id);
                    if (gtKey == null) {
                        NameValuePairs nvp = new NameValuePairs();
                        nvp.setAttribute("DlctTruongTtId", id);
                        nvp.setAttribute("DuyNhat", duyNhat);
                        Row row =
                            gtLckTtView1Iterator.getViewObject().createAndInitRow(nvp);
                        gtKey = row.getKey().toStringFormat(false);
                    }
                    if (formLayout != null) {
                        List<UIComponent> components =
                            createInputComponent(UPDATE_ID_PREFIX + gtKey, ten,
                                                 kieuDuLieu.intValue(),
                                                 batBuoc.intValue(), value);
                        if (components != null) {
                            formLayout.getChildren().addAll(components);
                        }
                    }
                }
            }

            dialogUpdate.setTitle((String)Utils.getElExpression("#{adfBundle['view.ViewControllerBundle'].CAP_NHAT}"));
            dialogUpdate.setAffirmativeTextAndAccessKey((String)Utils.getElExpression("#{adfBundle['view.ViewControllerBundle'].CAP_NHAT}"));
            Utils.showPopup(popupUpdate);
        }else{
            String errorString =
                Utils.getResourceBundle("CHON_BAN_GHI_DE_THAO_TAC");
            FacesContext.getCurrentInstance().addMessage(null,
                                                         new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                          errorString,
                                                                          errorString));
        }
    }

    public void showPopupXoa(ActionEvent actionEvent) {
        if (tableLoaiChungKhoan.getSelectedRowData() != null) {
            Utils.showPopup(popupDelete);
        }else{
            String errorString =
                Utils.getResourceBundle("CHON_BAN_GHI_DE_THAO_TAC");
            FacesContext.getCurrentInstance().addMessage(null,
                                                         new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                          errorString,
                                                                          errorString));
        }
    }

    public void removeFile(ActionEvent actionEvent) {
        String rowKey = actionEvent.getComponent().getId();
        if (rowKey.startsWith(UPDATE_ID_PREFIX))
            rowKey = rowKey.substring(UPDATE_ID_PREFIX.length());
        if (rowKey.endsWith(FILE_REMOVE_ID_SUBFIX))
            rowKey =
                    rowKey.substring(0, rowKey.length() - FILE_REMOVE_ID_SUBFIX.length());
        UIComponent groupComponent =
            actionEvent.getComponent().findComponent(UPDATE_ID_PREFIX +
                                                     rowKey +
                                                     FILE_GROUP_ID_SUBFIX);
        if (groupComponent != null &&
            groupComponent instanceof RichPanelGroupLayout) {
            RichPanelGroupLayout groupLayout =
                (RichPanelGroupLayout)groupComponent;
            groupLayout.setVisible(false);
            AdfFacesContext.getCurrentInstance().addPartialTarget(groupLayout);
        }

        RichInputFile inputFile =
            (RichInputFile)actionEvent.getComponent().findComponent(UPDATE_ID_PREFIX +
                                                                    rowKey);
        inputFile.resetValue();
        DCIteratorBinding gtLckTtView1Iterator =
            Utils.getIteratorBinding("DlctGtLckTtView1Iterator");
        gtLckTtView1Iterator.setCurrentRowWithKey(rowKey);
        gtLckTtView1Iterator.getCurrentRow().setAttribute("ValNumber",
                                                          new Number(Utils.ENUM_TRANG_THAI.KHONG_CONG_BO.getId()));
    }

    public void timKiem(ActionEvent actionEvent) {
        if (searchPanel.getChildren().size() > 0) {
            iteratorFilter();
            getValue(0);
            AdfFacesContext.getCurrentInstance().addPartialTarget(tableLoaiChungKhoan);
            AdfFacesContext.getCurrentInstance().partialUpdateNotify(tableLoaiChungKhoan);
        }
    }

    public void iteratorFilter() {
        if (searchPanel.getChildren().size() > 0) {
            {
                List<AdvancedSearchGroup> listASG = new ArrayList();
                AdvancedSearchGroup asg = new AdvancedSearchGroup();
                listASG.add(asg);
                AdvancedSearchItem asi = new AdvancedSearchItem();
                asg.getListChildren().add(asi);
                asi.setItemName("DlctLoaiTruongTt");
                asi.setRequired(true);
                asi.getListValue().add(0,
                                       new Number(Utils.ENUM_LOAI_TRUONG_TT.LOAI_CHUNG_KHOAN.getId()));
                asi = new AdvancedSearchItem();
                asg.getListChildren().add(asi);
                asi.setItemName("TrangThai");
                asi.setRequired(true);
                asi.getListValue().add(0,
                                       new Number(Utils.ENUM_TRANG_THAI.CONG_BO.getId()));
                Utils.searchadvanced("DlctTruongTtView1Iterator", listASG);
            }
            DCIteratorBinding truongTtView1Iterator =
                Utils.getIteratorBinding("DlctTruongTtView1Iterator");
            List<UIComponent> listUIComponent = new ArrayList();
            List<UIComponent> listDetailItem = new ArrayList();
            List<UIComponent> listDetailItemNext = new ArrayList();
            listDetailItemNext.add(searchPanel);
            while (listDetailItemNext.size() > 0) {
                listDetailItem = listDetailItemNext;
                listDetailItemNext = new ArrayList();
                for (UIComponent loopComponent : listDetailItem) {
                    if (loopComponent != null) {
                        if (loopComponent instanceof RichPanelTabbed ||
                            loopComponent instanceof RichShowDetailItem ||
                            loopComponent instanceof RichPanelFormLayout) {
                            if (loopComponent.getChildCount() > 0) {
                                listDetailItemNext.addAll(loopComponent.getChildren());
                            }
                        }
                        if (loopComponent instanceof RichInputText ||
                            loopComponent instanceof RichInputDate) {
                            listUIComponent.add(loopComponent);
                        }
                    }
                }
            }
            List<AdvancedSearchGroup> listASG = new ArrayList();
            for (UIComponent ui : listUIComponent) {
                if (ui.getId() != null) {
                    String rowKey = ui.getId();
                    if (rowKey.startsWith(SEARCH_ID_PREFIX))
                        rowKey = rowKey.substring(SEARCH_ID_PREFIX.length());
                    truongTtView1Iterator.setCurrentRowWithKey(rowKey);
                    Row truongTtRow = truongTtView1Iterator.getCurrentRow();
                    if (truongTtRow != null) {
                    Row loopRow = truongTtRow;
                    Row rootRow = null;
                    while (loopRow != null) {
                        rootRow = loopRow;
                        loopRow =
                                (Row)loopRow.getAttribute("DlctTruongTtTuIdDlctTruongTtView");
                    }
                    Number id =
                        ((DBSequence)rootRow.getAttribute("Id")).getSequenceNumber();
                        Number kieuDuLieu =
                            (Number)truongTtRow.getAttribute("KieuDuLieu");
                        if (kieuDuLieu != null) {
                            int kieu = kieuDuLieu.intValue();
                            if (kieu ==
                                Utils.ENUM_KIEU_DU_LIEU.NUMBER.getId()) {
                                RichInputText inputText = (RichInputText)ui;
                                Object valObject = inputText.getValue();
                                if (valObject != null &&
                                    valObject.toString().isEmpty() == false) {
                                    try {
                                        Number valNumber =
                                            new Number(valObject.toString().trim());
                                        AdvancedSearchGroup asg =
                                            new AdvancedSearchGroup();
                                        listASG.add(asg);
                                        AdvancedSearchItem asi =
                                            new AdvancedSearchItem();
                                        asg.getListChildren().add(asi);
                                        asi.setItemName("DlctGtLckTtView");
                                        asi.setRequired(true);
                                        asi.setOperator(AdvancedSearchItem.OPER_EXISTS);
                                        AdvancedSearchGroup asgChild =
                                            new AdvancedSearchGroup();
                                        asgChild.setConjunction(AdvancedSearchGroup.VC_CONJ_OR);
                                        asi.getListChildren().add(asgChild);
                                        AdvancedSearchItem asiChild =
                                            new AdvancedSearchItem();
                                        asgChild.getListChildren().add(asiChild);
                                        asiChild.setItemName("DlctTruongTtId");
                                        asiChild.setRequired(true);
                                        asiChild.getListValue().add(id);
                                        asiChild = new AdvancedSearchItem();
                                        asgChild.getListChildren().add(asiChild);
                                        asiChild.setItemName("ValNumber");
                                        asiChild.setRequired(true);
                                        asiChild.getListValue().add(valNumber);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                        String errorString =
                                            Utils.getResourceBundle("GIA_TRI_KHONG_HOP_LE");
                                        FacesContext.getCurrentInstance().addMessage(ui.getClientId(FacesContext.getCurrentInstance()),
                                                                                     new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                                                      errorString,
                                                                                                      errorString));
                                    }
                                }
                            }
                            if (kieu ==
                                Utils.ENUM_KIEU_DU_LIEU.VARCHAR.getId()) {
                                RichInputText inputText = (RichInputText)ui;
                                Object valObject = inputText.getValue();
                                if (valObject != null &&
                                    valObject.toString().isEmpty() == false) {
                                    try {
                                        String valString = valObject.toString().trim();
                                        valString = valString.toUpperCase();
                                        AdvancedSearchGroup asg =
                                            new AdvancedSearchGroup();
                                        listASG.add(asg);
                                        AdvancedSearchItem asi =
                                            new AdvancedSearchItem();
                                        asg.getListChildren().add(asi);
                                        asi.setItemName("DlctGtLckTtView");
                                        asi.setRequired(true);
                                        asi.setOperator(AdvancedSearchItem.OPER_EXISTS);
                                        AdvancedSearchGroup asgChild =
                                            new AdvancedSearchGroup();
                                        asgChild.setConjunction(AdvancedSearchGroup.VC_CONJ_OR);
                                        asi.getListChildren().add(asgChild);
                                        AdvancedSearchItem asiChild =
                                            new AdvancedSearchItem();
                                        asgChild.getListChildren().add(asiChild);
                                        asiChild.setItemName("DlctTruongTtId");
                                        asiChild.setRequired(true);
                                        asiChild.getListValue().add(id);
                                        asiChild = new AdvancedSearchItem();
                                        asgChild.getListChildren().add(asiChild);
                                        asiChild.setItemName("ValVarchar");
                                        asiChild.setOperator(AdvancedSearchItem.OPER_CONTAINS);
                                        asiChild.setUpperColumns(true);
                                        asiChild.setRequired(true);
                                        asiChild.getListValue().add(valString);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                        String errorString =
                                            Utils.getResourceBundle("GIA_TRI_KHONG_HOP_LE");
                                        FacesContext.getCurrentInstance().addMessage(ui.getClientId(FacesContext.getCurrentInstance()),
                                                                                     new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                                                      errorString,
                                                                                                      errorString));
                                    }
                                }
                            }
                            if (kieu == Utils.ENUM_KIEU_DU_LIEU.LINK.getId()) {
                                RichInputText inputText = (RichInputText)ui;
                                Object valObject = inputText.getValue();
                                if (valObject != null &&
                                    valObject.toString().isEmpty() == false) {
                                    try {
                                        String valString = valObject.toString().trim();
                                        valString = valString.toUpperCase();
                                        AdvancedSearchGroup asg =
                                            new AdvancedSearchGroup();
                                        listASG.add(asg);
                                        AdvancedSearchItem asi =
                                            new AdvancedSearchItem();
                                        asg.getListChildren().add(asi);
                                        asi.setItemName("DlctGtLckTtView");
                                        asi.setRequired(true);
                                        asi.setOperator(AdvancedSearchItem.OPER_EXISTS);
                                        AdvancedSearchGroup asgChild =
                                            new AdvancedSearchGroup();
                                        asgChild.setConjunction(AdvancedSearchGroup.VC_CONJ_OR);
                                        asi.getListChildren().add(asgChild);
                                        AdvancedSearchItem asiChild =
                                            new AdvancedSearchItem();
                                        asgChild.getListChildren().add(asiChild);
                                        asiChild.setItemName("DlctTruongTtId");
                                        asiChild.setRequired(true);
                                        asiChild.getListValue().add(id);
                                        asiChild = new AdvancedSearchItem();
                                        asgChild.getListChildren().add(asiChild);
                                        asiChild.setItemName("ValVarchar");
                                        asiChild.setOperator(AdvancedSearchItem.OPER_CONTAINS);
                                        asiChild.setUpperColumns(true);
                                        asiChild.setRequired(true);
                                        asiChild.getListValue().add(valString);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                        String errorString =
                                            Utils.getResourceBundle("GIA_TRI_KHONG_HOP_LE");
                                        FacesContext.getCurrentInstance().addMessage(ui.getClientId(FacesContext.getCurrentInstance()),
                                                                                     new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                                                      errorString,
                                                                                                      errorString));
                                    }
                                }
                            }
                            if (kieu ==
                                Utils.ENUM_KIEU_DU_LIEU.EMAIL.getId()) {
                                RichInputText inputText = (RichInputText)ui;
                                Object valObject = inputText.getValue();
                                if (valObject != null &&
                                    valObject.toString().isEmpty() == false) {
                                    try {
                                        String valString = valObject.toString().trim();
                                        valString = valString.toUpperCase();
                                        AdvancedSearchGroup asg =
                                            new AdvancedSearchGroup();
                                        listASG.add(asg);
                                        AdvancedSearchItem asi =
                                            new AdvancedSearchItem();
                                        asg.getListChildren().add(asi);
                                        asi.setItemName("DlctGtLckTtView");
                                        asi.setRequired(true);
                                        asi.setOperator(AdvancedSearchItem.OPER_EXISTS);
                                        AdvancedSearchGroup asgChild =
                                            new AdvancedSearchGroup();
                                        asgChild.setConjunction(AdvancedSearchGroup.VC_CONJ_OR);
                                        asi.getListChildren().add(asgChild);
                                        AdvancedSearchItem asiChild =
                                            new AdvancedSearchItem();
                                        asgChild.getListChildren().add(asiChild);
                                        asiChild.setItemName("DlctTruongTtId");
                                        asiChild.setRequired(true);
                                        asiChild.getListValue().add(id);
                                        asiChild = new AdvancedSearchItem();
                                        asgChild.getListChildren().add(asiChild);
                                        asiChild.setItemName("ValVarchar");
                                        asiChild.setOperator(AdvancedSearchItem.OPER_CONTAINS);
                                        asiChild.setUpperColumns(true);
                                        asiChild.setRequired(true);
                                        asiChild.getListValue().add(valString);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                        String errorString =
                                            Utils.getResourceBundle("GIA_TRI_KHONG_HOP_LE");
                                        FacesContext.getCurrentInstance().addMessage(ui.getClientId(FacesContext.getCurrentInstance()),
                                                                                     new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                                                      errorString,
                                                                                                      errorString));
                                    }
                                }
                            }
                            if (kieu == Utils.ENUM_KIEU_DU_LIEU.DATE.getId()) {
                                RichInputDate inputDate = (RichInputDate)ui;
                                Object valObject = inputDate.getValue();
                                if (valObject != null &&
                                    valObject.toString().isEmpty() == false) {
                                    try {
                                        java.util.Date javaDate =
                                            (java.util.Date)valObject;
                                        Timestamp timestamp =
                                            new Timestamp(javaDate.getTime());
                                        oracle.jbo.domain.Date valDate =
                                            new oracle.jbo.domain.Date(timestamp);
                                        AdvancedSearchGroup asg =
                                            new AdvancedSearchGroup();
                                        listASG.add(asg);
                                        AdvancedSearchItem asi =
                                            new AdvancedSearchItem();
                                        asg.getListChildren().add(asi);
                                        asi.setItemName("DlctGtLckTtView");
                                        asi.setRequired(true);
                                        asi.setOperator(AdvancedSearchItem.OPER_EXISTS);
                                        AdvancedSearchGroup asgChild =
                                            new AdvancedSearchGroup();
                                        asgChild.setConjunction(AdvancedSearchGroup.VC_CONJ_OR);
                                        asi.getListChildren().add(asgChild);
                                        AdvancedSearchItem asiChild =
                                            new AdvancedSearchItem();
                                        asgChild.getListChildren().add(asiChild);
                                        asiChild.setItemName("DlctTruongTtId");
                                        asiChild.setRequired(true);
                                        asiChild.getListValue().add(id);
                                        asiChild = new AdvancedSearchItem();
                                        asgChild.getListChildren().add(asiChild);
                                        asiChild.setItemName("ValDate");
                                        asiChild.setRequired(true);
                                        asiChild.getListValue().add(valDate);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                        String errorString =
                                            Utils.getResourceBundle("GIA_TRI_KHONG_HOP_LE");
                                        FacesContext.getCurrentInstance().addMessage(ui.getClientId(FacesContext.getCurrentInstance()),
                                                                                     new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                                                      errorString,
                                                                                                      errorString));
                                    }
                                }
                            }
                            if (kieu == Utils.ENUM_KIEU_DU_LIEU.CLOB.getId()) {
                                RichInputText inputText = (RichInputText)ui;
                                Object valObject = inputText.getValue();
                                if (valObject != null &&
                                    valObject.toString().isEmpty() == false) {
                                    try {
                                        String valString = (String)valObject;
                                        valString = valString.toUpperCase();
                                        AdvancedSearchGroup asg =
                                            new AdvancedSearchGroup();
                                        listASG.add(asg);
                                        AdvancedSearchItem asi =
                                            new AdvancedSearchItem();
                                        asg.getListChildren().add(asi);
                                        asi.setItemName("DlctGtLckTtView");
                                        asi.setRequired(true);
                                        asi.setOperator(AdvancedSearchItem.OPER_EXISTS);
                                        AdvancedSearchGroup asgChild =
                                            new AdvancedSearchGroup();
                                        asgChild.setConjunction(AdvancedSearchGroup.VC_CONJ_OR);
                                        asi.getListChildren().add(asgChild);
                                        AdvancedSearchItem asiChild =
                                            new AdvancedSearchItem();
                                        asgChild.getListChildren().add(asiChild);
                                        asiChild.setItemName("DlctTruongTtId");
                                        asiChild.setRequired(true);
                                        asiChild.getListValue().add(id);
                                        asiChild = new AdvancedSearchItem();
                                        asgChild.getListChildren().add(asiChild);
                                        asiChild.setItemName("ValClob");
                                        asiChild.setOperator(AdvancedSearchItem.OPER_CONTAINS);
                                        asiChild.setUpperColumns(true);
                                        asiChild.setRequired(true);
                                        asiChild.getListValue().add(valString);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                        String errorString =
                                            Utils.getResourceBundle("GIA_TRI_KHONG_HOP_LE");
                                        FacesContext.getCurrentInstance().addMessage(ui.getClientId(FacesContext.getCurrentInstance()),
                                                                                     new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                                                      errorString,
                                                                                                      errorString));
                                    }
                                }
                            }
                            if (kieu == Utils.ENUM_KIEU_DU_LIEU.FILE.getId()) {
                                RichInputText inputText = (RichInputText)ui;
                                Object valObject = inputText.getValue();
                                if (valObject != null &&
                                    valObject.toString().isEmpty() == false) {
                                    try {
                                        String valString = (String)valObject;
                                        valString = valString.toUpperCase();
                                        AdvancedSearchGroup asg =
                                            new AdvancedSearchGroup();
                                        listASG.add(asg);
                                        AdvancedSearchItem asi =
                                            new AdvancedSearchItem();
                                        asg.getListChildren().add(asi);
                                        asi.setItemName("DlctGtLckTtView");
                                        asi.setRequired(true);
                                        asi.setOperator(AdvancedSearchItem.OPER_EXISTS);
                                        AdvancedSearchGroup asgChild =
                                            new AdvancedSearchGroup();
                                        asgChild.setConjunction(AdvancedSearchGroup.VC_CONJ_OR);
                                        asi.getListChildren().add(asgChild);
                                        AdvancedSearchItem asiChild =
                                            new AdvancedSearchItem();
                                        asgChild.getListChildren().add(asiChild);
                                        asiChild.setItemName("DlctTruongTtId");
                                        asiChild.setRequired(true);
                                        asiChild.getListValue().add(id);
                                        asiChild = new AdvancedSearchItem();
                                        asgChild.getListChildren().add(asiChild);
                                        asiChild.setItemName("HtFileView");
                                        asiChild.setOperator(AdvancedSearchItem.OPER_EXISTS);
                                        asiChild.setRequired(true);

                                        AdvancedSearchGroup asgFile =
                                            new AdvancedSearchGroup();
                                        asiChild.getListChildren().add(asgFile);
                                        AdvancedSearchItem asiFile =
                                            new AdvancedSearchItem();
                                        asgFile.getListChildren().add(asiFile);
                                        asiFile.setItemName("TenFile");
                                        asiFile.setRequired(true);
                                        asiFile.getListValue().add(valString);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                        String errorString =
                                            Utils.getResourceBundle("GIA_TRI_KHONG_HOP_LE");
                                        FacesContext.getCurrentInstance().addMessage(ui.getClientId(FacesContext.getCurrentInstance()),
                                                                                     new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                                                      errorString,
                                                                                                      errorString));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            Utils.searchadvanced("DlctLoaiChungKhoanView1Iterator", listASG);
        }
    }

    public void firstPage(ActionEvent actionEvent) {
        getValue(1);
    }

    public void prevPage(ActionEvent actionEvent) {
        Integer integer = null;
        try {
            integer = new Integer(goToPage.getValue().toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (integer != null) {
            getValue(integer.intValue() - 1);
        }
    }

    public void nextPage(ActionEvent actionEvent) {
        Integer integer = null;
        try {
            integer = new Integer(goToPage.getValue().toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (integer != null) {
            getValue(integer.intValue() + 1);
        }
    }

    public void lastPage(ActionEvent actionEvent) {
        getValue(999999);
    }

    public void changePage(ValueChangeEvent valueChangeEvent) {
        if (valueChangeEvent.getNewValue() != null) {
            if (valueChangeEvent.getOldValue() != null &&
                valueChangeEvent.getNewValue().toString().equals(valueChangeEvent.getOldValue().toString())) {
                return;
            }
            Integer integer = null;
            try {
                integer =
                        new Integer(valueChangeEvent.getNewValue().toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (integer != null) {
                getValue(integer.intValue());
            }
        }
    }

    public void changeUpdateField(ValueChangeEvent valueChangeEvent) {
        String rowKey = valueChangeEvent.getComponent().getId();
        if (rowKey.startsWith(UPDATE_ID_PREFIX))
            rowKey = rowKey.substring(UPDATE_ID_PREFIX.length());
        DCIteratorBinding gtLckTtView1Iterator =
            Utils.getIteratorBinding("DlctGtLckTtView1Iterator");
        gtLckTtView1Iterator.setCurrentRowWithKey(rowKey);
        System.out.println(gtLckTtView1Iterator.getCurrentRowKeyString());
        Row truongTtRow =
            (Row)gtLckTtView1Iterator.getCurrentRow().getAttribute("DlctTruongTtView");
        if (truongTtRow != null) {
            Number kieuDuLieu = (Number)truongTtRow.getAttribute("KieuDuLieu");
            if (kieuDuLieu != null) {
                int kieu = kieuDuLieu.intValue();
                Object valObject = valueChangeEvent.getNewValue();
                if (kieu == Utils.ENUM_KIEU_DU_LIEU.NUMBER.getId()) {
                    if (valObject != null &&
                        valObject.toString().isEmpty() == false) {
                        try {
                            Number valNumber = new Number(valObject.toString().trim());
                            gtLckTtView1Iterator.getCurrentRow().setAttribute("ValNumber",
                                                                              valNumber);
                            try {
                                gtLckTtView1Iterator.getCurrentRow().validate();
                            } catch (RowValException ex) {
                                boolean duyNhat=false;
                                Object[]  objs = ex.getDetails();
                                if (objs != null) {
                                    for (Object obj : objs) {
                                        if (obj instanceof RowValException ){
                                            RowValException e = (RowValException)obj;
                                            Object[] detailObjs=e.getErrorParameters();
                                            for (Object detailObj : detailObjs) {
                                                if (detailObj instanceof String &&
                                                    "validateDuyNhat".equals(detailObj)) {
                                                    duyNhat=true;
                                                    String errorString =
                                                        Utils.getResourceBundle("GIA_TRI_KHONG_DUY_NHAT");
                                                    FacesContext.getCurrentInstance().addMessage(valueChangeEvent.getComponent().getClientId(FacesContext.getCurrentInstance()),
                                                                                                 new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                                                                  errorString,
                                                                                                                  errorString));
                                                }
                                            }
                                        }
                                    }
                                }
                                if(duyNhat==false){
                                    throw ex;
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            String errorString =
                                Utils.getResourceBundle("GIA_TRI_KHONG_HOP_LE");
                            FacesContext.getCurrentInstance().addMessage(valueChangeEvent.getComponent().getClientId(FacesContext.getCurrentInstance()),
                                                                         new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                                          errorString,
                                                                                          errorString));
                        }
                    } else {
                        gtLckTtView1Iterator.getCurrentRow().setAttribute("ValNumber",
                                                                          null);
                    }
                }
                if (kieu == Utils.ENUM_KIEU_DU_LIEU.VARCHAR.getId()) {
                    if (valObject != null &&
                        valObject.toString().isEmpty() == false) {
                        try {
                            String valString = valObject.toString().trim();
                            gtLckTtView1Iterator.getCurrentRow().setAttribute("ValVarchar",
                                                                              valString);
                            try {
                                gtLckTtView1Iterator.getCurrentRow().validate();
                            } catch (RowValException ex) {
                                boolean duyNhat=false;
                                Object[]  objs = ex.getDetails();
                                if (objs != null) {
                                    for (Object obj : objs) {
                                        if (obj instanceof RowValException ){
                                            RowValException e = (RowValException)obj;
                                            Object[] detailObjs=e.getErrorParameters();
                                            for (Object detailObj : detailObjs) {
                                                if (detailObj instanceof String &&
                                                    "validateDuyNhat".equals(detailObj)) {
                                                    duyNhat=true;
                                                    String errorString =
                                                        Utils.getResourceBundle("GIA_TRI_KHONG_DUY_NHAT");
                                                    FacesContext.getCurrentInstance().addMessage(valueChangeEvent.getComponent().getClientId(FacesContext.getCurrentInstance()),
                                                                                                 new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                                                                  errorString,
                                                                                                                  errorString));
                                                }
                                            }
                                        }
                                    }
                                }
                                if(duyNhat==false){
                                    throw ex;
                                }
                            }
                            String expression=(String)truongTtRow.getAttribute("Expression");
                            if(expression!=null&&expression.isEmpty()==false){
                                if(valString.matches(expression)==false){
                            String errorString =
                                Utils.getResourceBundle("GIA_TRI_KHONG_HOP_LE");
                            FacesContext.getCurrentInstance().addMessage(valueChangeEvent.getComponent().getClientId(FacesContext.getCurrentInstance()),
                                                                         new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                                          errorString,
                                                                                          errorString));
                                }                                 
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            String errorString =
                                Utils.getResourceBundle("GIA_TRI_KHONG_HOP_LE");
                            FacesContext.getCurrentInstance().addMessage(valueChangeEvent.getComponent().getClientId(FacesContext.getCurrentInstance()),
                                                                         new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                                          errorString,
                                                                                          errorString));
                        }
                    } else {
                        gtLckTtView1Iterator.getCurrentRow().setAttribute("ValVarchar",
                                                                          null);
                    }
                }
                if (kieu == Utils.ENUM_KIEU_DU_LIEU.LINK.getId()) {
                    if (valObject != null &&
                        valObject.toString().isEmpty() == false) {
                        try {
                            String valString = valObject.toString().trim();
                            gtLckTtView1Iterator.getCurrentRow().setAttribute("ValVarchar",
                                                                              valString);
                            try {
                                gtLckTtView1Iterator.getCurrentRow().validate();
                            } catch (RowValException ex) {
                                boolean duyNhat=false;
                                Object[]  objs = ex.getDetails();
                                if (objs != null) {
                                    for (Object obj : objs) {
                                        if (obj instanceof RowValException ){
                                            RowValException e = (RowValException)obj;
                                            Object[] detailObjs=e.getErrorParameters();
                                            for (Object detailObj : detailObjs) {
                                                if (detailObj instanceof String &&
                                                    "validateDuyNhat".equals(detailObj)) {
                                                    duyNhat=true;
                                                    String errorString =
                                                        Utils.getResourceBundle("GIA_TRI_KHONG_DUY_NHAT");
                                                    FacesContext.getCurrentInstance().addMessage(valueChangeEvent.getComponent().getClientId(FacesContext.getCurrentInstance()),
                                                                                                 new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                                                                  errorString,
                                                                                                                  errorString));
                                                }
                                            }
                                        }
                                    }
                                }
                                if(duyNhat==false){
                                    throw ex;
                                }
                            }
                            String expression=(String)truongTtRow.getAttribute("Expression");
                            if(expression!=null&&expression.isEmpty()==false){
                                if(valString.matches(expression)==false){
                            String errorString =
                                Utils.getResourceBundle("GIA_TRI_KHONG_HOP_LE");
                            FacesContext.getCurrentInstance().addMessage(valueChangeEvent.getComponent().getClientId(FacesContext.getCurrentInstance()),
                                                                         new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                                          errorString,
                                                                                          errorString));
                                }                                 
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            String errorString =
                                Utils.getResourceBundle("GIA_TRI_KHONG_HOP_LE");
                            FacesContext.getCurrentInstance().addMessage(valueChangeEvent.getComponent().getClientId(FacesContext.getCurrentInstance()),
                                                                         new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                                          errorString,
                                                                                          errorString));
                        }
                    } else {
                        gtLckTtView1Iterator.getCurrentRow().setAttribute("ValVarchar",
                                                                          null);
                    }
                }
                if (kieu == Utils.ENUM_KIEU_DU_LIEU.EMAIL.getId()) {
                    if (valObject != null &&
                        valObject.toString().isEmpty() == false) {
                        try {
                            String valString = valObject.toString().trim();
                            gtLckTtView1Iterator.getCurrentRow().setAttribute("ValVarchar",
                                                                              valString);
                            try {
                                gtLckTtView1Iterator.getCurrentRow().validate();
                            } catch (RowValException ex) {
                                boolean duyNhat=false;
                                Object[]  objs = ex.getDetails();
                                if (objs != null) {
                                    for (Object obj : objs) {
                                        if (obj instanceof RowValException ){
                                            RowValException e = (RowValException)obj;
                                            Object[] detailObjs=e.getErrorParameters();
                                            for (Object detailObj : detailObjs) {
                                                if (detailObj instanceof String &&
                                                    "validateDuyNhat".equals(detailObj)) {
                                                    duyNhat=true;
                                                    String errorString =
                                                        Utils.getResourceBundle("GIA_TRI_KHONG_DUY_NHAT");
                                                    FacesContext.getCurrentInstance().addMessage(valueChangeEvent.getComponent().getClientId(FacesContext.getCurrentInstance()),
                                                                                                 new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                                                                  errorString,
                                                                                                                  errorString));
                                                }
                                            }
                                        }
                                    }
                                }
                                if(duyNhat==false){
                                    throw ex;
                                }
                            }
                            String expression=(String)truongTtRow.getAttribute("Expression");
                            if(expression!=null&&expression.isEmpty()==false){
                                if(valString.matches(expression)==false){
                            String errorString =
                                Utils.getResourceBundle("GIA_TRI_KHONG_HOP_LE");
                            FacesContext.getCurrentInstance().addMessage(valueChangeEvent.getComponent().getClientId(FacesContext.getCurrentInstance()),
                                                                         new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                                          errorString,
                                                                                          errorString));
                                }                                 
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            String errorString =
                                Utils.getResourceBundle("GIA_TRI_KHONG_HOP_LE");
                            FacesContext.getCurrentInstance().addMessage(valueChangeEvent.getComponent().getClientId(FacesContext.getCurrentInstance()),
                                                                         new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                                          errorString,
                                                                                          errorString));
                        }
                    } else {
                        gtLckTtView1Iterator.getCurrentRow().setAttribute("ValVarchar",
                                                                          null);
                    }
                }
                if (kieu == Utils.ENUM_KIEU_DU_LIEU.DATE.getId()) {
                    if (valObject != null &&
                        valObject.toString().isEmpty() == false) {
                        try {
                            java.util.Date javaDate =
                                (java.util.Date)valObject;
                            Timestamp timestamp =
                                new Timestamp(javaDate.getTime());
                            oracle.jbo.domain.Date valDate =
                                new oracle.jbo.domain.Date(timestamp);
                            gtLckTtView1Iterator.getCurrentRow().setAttribute("ValDate",
                                                                              valDate);
                            try {
                                gtLckTtView1Iterator.getCurrentRow().validate();
                            } catch (RowValException ex) {
                                boolean duyNhat=false;
                                Object[]  objs = ex.getDetails();
                                if (objs != null) {
                                    for (Object obj : objs) {
                                        if (obj instanceof RowValException ){
                                            RowValException e = (RowValException)obj;
                                            Object[] detailObjs=e.getErrorParameters();
                                            for (Object detailObj : detailObjs) {
                                                if (detailObj instanceof String &&
                                                    "validateDuyNhat".equals(detailObj)) {
                                                    duyNhat=true;
                                                    String errorString =
                                                        Utils.getResourceBundle("GIA_TRI_KHONG_DUY_NHAT");
                                                    FacesContext.getCurrentInstance().addMessage(valueChangeEvent.getComponent().getClientId(FacesContext.getCurrentInstance()),
                                                                                                 new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                                                                  errorString,
                                                                                                                  errorString));
                                                }
                                            }
                                        }
                                    }
                                }
                                if(duyNhat==false){
                                    throw ex;
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            String errorString =
                                Utils.getResourceBundle("GIA_TRI_KHONG_HOP_LE");
                            FacesContext.getCurrentInstance().addMessage(valueChangeEvent.getComponent().getClientId(FacesContext.getCurrentInstance()),
                                                                         new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                                          errorString,
                                                                                          errorString));
                        }
                    } else {

                        gtLckTtView1Iterator.getCurrentRow().setAttribute("ValDate",
                                                                          null);
                    }
                }
                if (kieu == Utils.ENUM_KIEU_DU_LIEU.CLOB.getId()) {
                    if (valObject != null &&
                        valObject.toString().isEmpty() == false) {
                        try {
                            String valString = (String)valObject;
                            ClobDomain valClob = new ClobDomain(valString);
                            gtLckTtView1Iterator.getCurrentRow().remove();
                            NameValuePairs nvp = new NameValuePairs();
                            nvp.setAttribute("DlctTruongTtId",
                                             ((DBSequence)truongTtRow.getAttribute("Id")).getSequenceNumber());
                            nvp.setAttribute("DuyNhat",
                                             truongTtRow.getAttribute("DuyNhat"));
                            nvp.setAttribute("ValClob", valClob);
                            Row row=gtLckTtView1Iterator.getViewObject().createAndInitRow(nvp);
                            valueChangeEvent.getComponent().setId(UPDATE_ID_PREFIX+row.getKey().toStringFormat(false));
                            gtLckTtView1Iterator.executeQuery();
                        } catch (Exception e) {
                            e.printStackTrace();
                            String errorString =
                                Utils.getResourceBundle("GIA_TRI_KHONG_HOP_LE");
                            FacesContext.getCurrentInstance().addMessage(valueChangeEvent.getComponent().getClientId(FacesContext.getCurrentInstance()),
                                                                         new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                                          errorString,
                                                                                          errorString));
                        }
                    } else {
                            gtLckTtView1Iterator.getCurrentRow().remove();
                            NameValuePairs nvp = new NameValuePairs();
                            nvp.setAttribute("DlctTruongTtId",
                                             ((DBSequence)truongTtRow.getAttribute("Id")).getSequenceNumber());
                            nvp.setAttribute("DuyNhat",
                                             truongTtRow.getAttribute("DuyNhat"));
                            Row row=gtLckTtView1Iterator.getViewObject().createAndInitRow(nvp);
                            valueChangeEvent.getComponent().setId(UPDATE_ID_PREFIX+row.getKey().toStringFormat(false));
                            gtLckTtView1Iterator.executeQuery();
                    }
                }
                if (kieu == Utils.ENUM_KIEU_DU_LIEU.FILE.getId()) {
                    UploadedFile file = (UploadedFile)valObject;
                    if (file != null) {
                        if(Utils.checkDefaultFileExtension(file.getFilename())==false){
                            String errorString =
                                Utils.getResourceBundle("SAI_DINH_DANG_FILE") +
                                "(" + Utils.getDefaultFileExtension() + ")";
                            ((RichInputFile)valueChangeEvent.getComponent()).resetValue();
                            FacesContext.getCurrentInstance().addMessage(valueChangeEvent.getComponent().getClientId(FacesContext.getCurrentInstance()),
                                                                         new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                                          errorString,
                                                                                          errorString));
                        }else{
                        try {
                            BlobDomain blob = new BlobDomain();
                            InputStream in = file.getInputStream();
                            OutputStream out = blob.getBinaryOutputStream();
                            byte[] buffer = new byte[1024];
                            int size = 0;
                            while ((size = in.read(buffer)) != -1) {
                                out.write(buffer, 0, size);
                            }
                            in.close();
                            out.flush();
                            gtLckTtView1Iterator.getCurrentRow().setAttribute("ValVarchar",
                                                                              file.getFilename());
                            gtLckTtView1Iterator.getCurrentRow().setAttribute("ValNumber",
                                                                              null);
                            gtLckTtView1Iterator.getCurrentRow().setAttribute("ValBlob",
                                                                              blob);
                            UIComponent downloadComponent =
                                valueChangeEvent.getComponent().findComponent(UPDATE_ID_PREFIX +
                                                                              rowKey +
                                                                              FILE_DOWNLOAD_ID_SUBFIX);
                            if (downloadComponent != null &&
                                downloadComponent instanceof RichCommandLink) {
                                RichCommandLink commandLink =
                                    (RichCommandLink)downloadComponent;
                                commandLink.setText(file.getFilename());
                                AdfFacesContext.getCurrentInstance().addPartialTarget(commandLink);
                            }
                            UIComponent groupComponent =
                                valueChangeEvent.getComponent().findComponent(UPDATE_ID_PREFIX +
                                                                              rowKey +
                                                                              FILE_GROUP_ID_SUBFIX);
                            if (groupComponent != null &&
                                groupComponent instanceof
                                RichPanelGroupLayout) {
                                RichPanelGroupLayout groupLayout =
                                    (RichPanelGroupLayout)groupComponent;
                                groupLayout.setVisible(true);
                                AdfFacesContext.getCurrentInstance().addPartialTarget(groupLayout);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            String errorString =
                                Utils.getResourceBundle("GIA_TRI_KHONG_HOP_LE");
                            FacesContext.getCurrentInstance().addMessage(valueChangeEvent.getComponent().getClientId(FacesContext.getCurrentInstance()),
                                                                         new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                                          errorString,
                                                                                          errorString));
                        }
                        }
                    } else {
                        gtLckTtView1Iterator.getCurrentRow().setAttribute("ValVarchar",
                                                                          null);
                        gtLckTtView1Iterator.getCurrentRow().setAttribute("ValNumber",
                                                                          null);
                        gtLckTtView1Iterator.getCurrentRow().setAttribute("ValBlob",
                                                                          null);
                        UIComponent groupComponent =
                            valueChangeEvent.getComponent().findComponent(UPDATE_ID_PREFIX +
                                                                          rowKey +
                                                                          FILE_GROUP_ID_SUBFIX);
                        if (groupComponent != null &&
                            groupComponent instanceof RichPanelGroupLayout) {
                            RichPanelGroupLayout groupLayout =
                                (RichPanelGroupLayout)groupComponent;
                            groupLayout.setVisible(false);
                            AdfFacesContext.getCurrentInstance().addPartialTarget(groupLayout);
                        }
                    }
                }
            }
        }
    }

    public void changeNgonNgu(ValueChangeEvent valueChangeEvent) {
        List<String> listNgonNguId =
            (List<String>)valueChangeEvent.getNewValue();
        createUpdateForm(listNgonNguId);
    }

    public void fileDownload(ActionEvent actionEvent) {
        String rowKey = actionEvent.getComponent().getId();
        if (rowKey.startsWith(UPDATE_ID_PREFIX))
            rowKey = rowKey.substring(UPDATE_ID_PREFIX.length());
        if (rowKey.endsWith(FILE_DOWNLOAD_ID_SUBFIX))
            rowKey =
                    rowKey.substring(0, rowKey.length() - FILE_DOWNLOAD_ID_SUBFIX.length());
        DCIteratorBinding gtLckTtView1Iterator =
            Utils.getIteratorBinding("DlctGtLckTtView1Iterator");
        gtLckTtView1Iterator.setCurrentRowWithKey(rowKey);
        Row truongTtRow =
            (Row)gtLckTtView1Iterator.getCurrentRow().getAttribute("DlctTruongTtView");
        if (truongTtRow != null) {
            Number id =
                ((DBSequence)truongTtRow.getAttribute("Id")).getSequenceNumber();
            Number kieuDuLieu = (Number)truongTtRow.getAttribute("KieuDuLieu");
            if (id != null && kieuDuLieu != null) {
                int kieu = kieuDuLieu.intValue();
                if (kieu == Utils.ENUM_KIEU_DU_LIEU.FILE.getId()) {
                    BlobDomain blob =
                        (BlobDomain)gtLckTtView1Iterator.getCurrentRow().getAttribute("ValBlob");
                    Row fileViewRow =
                        (Row)gtLckTtView1Iterator.getCurrentRow().getAttribute("HtFileView");
                    if (blob == null) {
                        if (fileViewRow != null) {
                            String duongDan =
                                (String)fileViewRow.getAttribute("DuongDan");
                            if (duongDan != null) {
                                String rootString =
                                    Utils.ENUM_CAU_HINH_KHOA.FILE_ROOT.getValue("HtCauHinhView1Iterator");
                                if (rootString != null) {
                                    File rootFile = new File(rootString);
                                    File thisFile =
                                        new File(rootFile, duongDan);
                                    if (thisFile.exists() == false) {
                                        System.out.println("file khong ton tai");
                                        return;
                                    }else{
                                        fileDownloadButton.getAttributes().put("rowKey", rowKey);
                                        fileDownloadButton.setText((String)fileViewRow.getAttribute("TenFile"));

                                        FacesContext context = FacesContext.getCurrentInstance();
                                        ExtendedRenderKitService erks =
                                            Service.getService(context.getRenderKit(),
                                                               ExtendedRenderKitService.class);

                                        erks.addScript(context, "customHandler();");
                                    }
                                }
                            }
                        } else {
                            return;
                        }
                    }else{
                        fileDownloadButton.getAttributes().put("rowKey", rowKey);
                        fileDownloadButton.setText((String)gtLckTtView1Iterator.getCurrentRow().getAttribute("ValVarchar"));

                        FacesContext context = FacesContext.getCurrentInstance();
                        ExtendedRenderKitService erks =
                            Service.getService(context.getRenderKit(),
                                               ExtendedRenderKitService.class);

                        erks.addScript(context, "customHandler();");
                    }
                }
            }
        }
    }

    public void fileDownload(FacesContext facesContext,
                             OutputStream outputStream) {
        String rowKey =
            (String)fileDownloadButton.getAttributes().get("rowKey");
        DCIteratorBinding gtLckTtView1Iterator =
            Utils.getIteratorBinding("DlctGtLckTtView1Iterator");
        gtLckTtView1Iterator.setCurrentRowWithKey(rowKey);
        Row truongTtRow =
            (Row)gtLckTtView1Iterator.getCurrentRow().getAttribute("DlctTruongTtView");
        if (truongTtRow != null) {
            Number id =
                ((DBSequence)truongTtRow.getAttribute("Id")).getSequenceNumber();
            Number kieuDuLieu = (Number)truongTtRow.getAttribute("KieuDuLieu");
            if (id != null && kieuDuLieu != null) {
                int kieu = kieuDuLieu.intValue();
                if (kieu == Utils.ENUM_KIEU_DU_LIEU.FILE.getId()) {
                    BlobDomain blob =
                        (BlobDomain)gtLckTtView1Iterator.getCurrentRow().getAttribute("ValBlob");
                    Row fileViewRow =
                        (Row)gtLckTtView1Iterator.getCurrentRow().getAttribute("HtFileView");
                    if (blob == null) {
                        if (fileViewRow != null) {
                            String duongDan =
                                (String)fileViewRow.getAttribute("DuongDan");
                            if (duongDan != null) {
                                String rootString =
                                    Utils.ENUM_CAU_HINH_KHOA.FILE_ROOT.getValue("HtCauHinhView1Iterator");
                                if (rootString != null) {
                                    File rootFile = new File(rootString);
                                    File thisFile =
                                        new File(rootFile, duongDan);
                                    if (thisFile.exists()) {
                                        try {
                                            FileInputStream in=new FileInputStream(thisFile);
                                            byte buffer[] = new byte[1024];
                                            int size = 0;
                                            while ((size = in.read(buffer)) != -1) {
                                                outputStream.write(buffer, 0, size);
                                            }
                                            in.close();
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        } finally {
                                            if (outputStream != null) {
                                                try {
                                                    outputStream.flush();
                                                    outputStream.close();
                                                } catch (Exception e) {
                                                    e.printStackTrace();
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        } 
                    }else{
                        
                        try {
                            InputStream in=
                            blob.getBinaryStream();
                            byte buffer[] = new byte[1024];
                            int size = 0;
                            while ((size = in.read(buffer)) != -1) {
                                outputStream.write(buffer, 0, size);
                            }
                            in.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            if (outputStream != null) {
                                try {
                                    outputStream.flush();
                                    outputStream.close();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    private void searchTruongTt() {
        List<AdvancedSearchGroup> listASG = new ArrayList();
        AdvancedSearchGroup asg = new AdvancedSearchGroup();
        listASG.add(asg);
        AdvancedSearchItem asi = new AdvancedSearchItem();
        asg.getListChildren().add(asi);
        asi.setItemName("DlctLoaiTruongTt");
        asi.setRequired(true);
        asi.getListValue().add(0,
                               new Number(Utils.ENUM_LOAI_TRUONG_TT.LOAI_CHUNG_KHOAN.getId()));
        asi = new AdvancedSearchItem();
        asg.getListChildren().add(asi);
        asi.setItemName("TrangThai");
        asi.setRequired(true);
        asi.getListValue().add(0,
                               new Number(Utils.ENUM_TRANG_THAI.CONG_BO.getId()));
        Utils.searchadvanced("DlctTruongTtView1Iterator", listASG);
    }

    private void createUpdateForm(List<String> listNgonNguId) {
        updatePanel.getChildren().clear();
        if (listNgonNguId != null && listNgonNguId.size() > 0) {
            updatePanel.setVisible(true);

            DCIteratorBinding ngonNguView1Iterator =
                Utils.getIteratorBinding("DlctNgonNguView1Iterator");
            DCIteratorBinding gtLckTtView1Iterator =
                Utils.getIteratorBinding("DlctGtLckTtView1Iterator");

            Map<String, RichPanelFormLayout> mapNgonNguLayout = new HashMap();
            for (String ngonNguId : listNgonNguId) {
                ngonNguView1Iterator.setCurrentRowWithKeyValue(ngonNguId);
                RichShowDetailItem showDetailItem = new RichShowDetailItem();
                showDetailItem.setImmediate(true);
                showDetailItem.setText((String)ngonNguView1Iterator.getCurrentRow().getAttribute("Ten"));
                RichPanelFormLayout formLayout = new RichPanelFormLayout();
                mapNgonNguLayout.put(ngonNguId, formLayout);
                showDetailItem.getChildren().add(formLayout);
                updatePanel.getChildren().add(showDetailItem);
            }

            Map<Number, Object> mapValue = new HashMap();
            Map<Number, String> mapGtKey = new HashMap();
            gtLckTtView1Iterator.setRangeSize(-1);
            Row[] rows = gtLckTtView1Iterator.getAllRowsInRange();
            if (rows == null) {
                rows = new Row[] { };
            }
            for (int i = 0; i < rows.length; i++) {
                Row truongTtRow =
                    (Row)rows[i].getAttribute("DlctTruongTtView");
                if (truongTtRow != null) {
                    Number id =
                        ((DBSequence)truongTtRow.getAttribute("Id")).getSequenceNumber();
                    Number kieuDuLieu =
                        (Number)truongTtRow.getAttribute("KieuDuLieu");
                    if (id != null && kieuDuLieu != null) {
                        int kieu = kieuDuLieu.intValue();
                        mapGtKey.put(id,
                                     rows[i].getKey().toStringFormat(false));
                        if (kieu == Utils.ENUM_KIEU_DU_LIEU.NUMBER.getId()) {
                            Number valNumber =
                                (Number)rows[i].getAttribute("ValNumber");
                            if (valNumber != null) {
                                mapValue.put(id, valNumber.toString());
                            }
                        }
                        if (kieu == Utils.ENUM_KIEU_DU_LIEU.VARCHAR.getId()) {
                            String valVarchar =
                                (String)rows[i].getAttribute("ValVarchar");
                            if (valVarchar != null) {
                                mapValue.put(id, valVarchar);
                            }
                        }
                        if (kieu == Utils.ENUM_KIEU_DU_LIEU.LINK.getId()) {
                            String valVarchar =
                                (String)rows[i].getAttribute("ValVarchar");
                            if (valVarchar != null) {
                                mapValue.put(id, valVarchar);
                            }
                        }
                        if (kieu == Utils.ENUM_KIEU_DU_LIEU.EMAIL.getId()) {
                            String valVarchar =
                                (String)rows[i].getAttribute("ValVarchar");
                            if (valVarchar != null) {
                                mapValue.put(id, valVarchar);
                            }
                        }
                        if (kieu == Utils.ENUM_KIEU_DU_LIEU.DATE.getId()) {
                            oracle.jbo.domain.Date valDate =
                                (oracle.jbo.domain.Date)rows[i].getAttribute("ValDate");
                            if (valDate != null) {
                                java.util.Date date = valDate.dateValue();
                                mapValue.put(id, date);
                            }
                        }
                        if (kieu == Utils.ENUM_KIEU_DU_LIEU.CLOB.getId()) {
                            ClobDomain valClob =
                                (ClobDomain)rows[i].getAttribute("ValClob");
                            if (valClob != null) {
                                mapValue.put(id, valClob.toString());
                            }
                        }
                        if (kieu == Utils.ENUM_KIEU_DU_LIEU.FILE.getId()) {
                            Row htFileRow =
                                (Row)rows[i].getAttribute("HtFileView");
                            if (htFileRow != null) {
                                mapValue.put(id,
                                             htFileRow.getAttribute("TenFile"));
                            }
                        }
                    }
                }
            }

            searchTruongTt();
            DCIteratorBinding truongTtView1Iterator =
                Utils.getIteratorBinding("DlctTruongTtView1Iterator");
            truongTtView1Iterator.setRangeSize(-1);
            rows = truongTtView1Iterator.getAllRowsInRange();
            if (rows == null) {
                rows = new Row[] { };
            }
            for (int i = 0; i < rows.length; i++) {
                Number id =
                    ((DBSequence)rows[i].getAttribute("Id")).getSequenceNumber();
                String ten = (String)rows[i].getAttribute("TenTruong");
                Number batBuoc = (Number)rows[i].getAttribute("BatBuoc");
                Number duyNhat = (Number)rows[i].getAttribute("DuyNhat");
                Number kieuDuLieu = (Number)rows[i].getAttribute("KieuDuLieu");
                Number ngonNguId =
                    (Number)rows[i].getAttribute("DlctNgonNguId");
                if (batBuoc != null && kieuDuLieu != null &&
                    ngonNguId != null) {
                    RichPanelFormLayout formLayout =
                        mapNgonNguLayout.get(String.valueOf(ngonNguId.intValue()));
                    String gtKey = mapGtKey.get(id);
                    Object value = mapValue.get(id);
                    if (gtKey == null) {
                        NameValuePairs nvp = new NameValuePairs();
                        nvp.setAttribute("DlctTruongTtId", id);
                        nvp.setAttribute("DuyNhat", duyNhat);
                        Row row =
                            gtLckTtView1Iterator.getViewObject().createAndInitRow(nvp);
                        gtKey = row.getKey().toStringFormat(false);
                        gtLckTtView1Iterator.executeQuery();
                    }
                    if (formLayout != null) {
                        List<UIComponent> components =
                            createInputComponent(UPDATE_ID_PREFIX + gtKey, ten,
                                                 kieuDuLieu.intValue(),
                                                 batBuoc.intValue(), value);
                        if (components != null) {
                            formLayout.getChildren().addAll(components);
                        }
                    }
                }
            }
        } else {
            updatePanel.setVisible(false);
        }
    }

    public void rollback(PopupCanceledEvent popupCanceledEvent) {
        DCIteratorBinding loaiChungKhoanView1Iterator =
            Utils.getIteratorBinding("DlctLoaiChungKhoanView1Iterator");
        loaiChungKhoanView1Iterator.getDataControl().getApplicationModule().getTransaction().rollback();
        String rowKey = null;
        if (loaiChungKhoanView1Iterator.getCurrentRow() != null)
            rowKey =
                    loaiChungKhoanView1Iterator.getCurrentRow().getKey().toStringFormat(false);
        iteratorFilter();
        if (rowKey != null)
            loaiChungKhoanView1Iterator.setCurrentRowWithKey(rowKey);
        Integer pageNum = null;
        try {
            pageNum = new Integer(goToPage.getValue().toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (pageNum != null) {
            getValue(pageNum.intValue());
        }
    }

    public void commit(DialogEvent dialogEvent) {
        DCIteratorBinding fileView1Iterator =
            Utils.getIteratorBinding("HtFileView1Iterator");
        DCIteratorBinding lckNnView1Iterator =
            Utils.getIteratorBinding("DlctLckNnView1Iterator");
        DCIteratorBinding gtLckTtView1Iterator =
            Utils.getIteratorBinding("DlctGtLckTtView1Iterator");
        List<String> listDelete = new ArrayList();
        gtLckTtView1Iterator.setRangeSize(-1);
        Row[] rows = gtLckTtView1Iterator.getAllRowsInRange();
        if (rows == null) {
            rows = new Row[] { };
        }
        List<UIComponent> listInvalid = new ArrayList();
        List<UIComponent> listUnique= new ArrayList();
        List<UIComponent> listRequired = new ArrayList();
        for (int i = 0; i < rows.length; i++) {
            UIComponent ui =
                updatePanel.findComponent(UPDATE_ID_PREFIX + rows[i].getKey().toStringFormat(false));
            Row truongTtRow = (Row)rows[i].getAttribute("DlctTruongTtView");
            if (truongTtRow != null && ui != null) {
                Number batBuoc = (Number)truongTtRow.getAttribute("BatBuoc");
                Number kieuDuLieu =
                    (Number)truongTtRow.getAttribute("KieuDuLieu");
                if (batBuoc != null && kieuDuLieu != null) {
                    Object value = null;
                    if (kieuDuLieu.intValue() ==
                        Utils.ENUM_KIEU_DU_LIEU.NUMBER.getId()) {
                        Number valNumber =
                            (Number)rows[i].getAttribute("ValNumber");
                        if (valNumber != null) {
                            value = valNumber.toString();
                        }
                    }
                    if (kieuDuLieu.intValue() ==
                        Utils.ENUM_KIEU_DU_LIEU.VARCHAR.getId()) {
                        String valVarchar =
                            (String)rows[i].getAttribute("ValVarchar");
                        if (valVarchar != null) {
                            value = valVarchar;
                        }
                    }
                    if (kieuDuLieu.intValue() ==
                        Utils.ENUM_KIEU_DU_LIEU.LINK.getId()) {
                        String valVarchar =
                            (String)rows[i].getAttribute("ValVarchar");
                        if (valVarchar != null) {
                            value = valVarchar;
                        }
                    }
                    if (kieuDuLieu.intValue() ==
                        Utils.ENUM_KIEU_DU_LIEU.EMAIL.getId()) {
                        String valVarchar =
                            (String)rows[i].getAttribute("ValVarchar");
                        if (valVarchar != null) {
                            value = valVarchar;
                        }
                    }
                    if (kieuDuLieu.intValue() ==
                        Utils.ENUM_KIEU_DU_LIEU.DATE.getId()) {
                        oracle.jbo.domain.Date valDate =
                            (oracle.jbo.domain.Date)rows[i].getAttribute("ValDate");
                        if (valDate != null) {
                            java.util.Date date = valDate.dateValue();
                            if (date != null) {
                                value = date;
                            }
                        }
                    }
                    if (kieuDuLieu.intValue() ==
                        Utils.ENUM_KIEU_DU_LIEU.CLOB.getId()) {
                        ClobDomain valClob =
                            (ClobDomain)rows[i].getAttribute("ValClob");
                        if (valClob != null) {
                            value = valClob.toString();
                        }
                    }
                    if (kieuDuLieu.intValue() ==
                        Utils.ENUM_KIEU_DU_LIEU.FILE.getId()) {
                        Number trangThai =
                            (Number)rows[i].getAttribute("ValNumber");
                        String tenFile =
                            (String)rows[i].getAttribute("ValVarchar");
                        if (trangThai == null ||
                            trangThai.intValue() == Utils.ENUM_TRANG_THAI.CONG_BO.getId()) {
                            if (tenFile != null) {
                                value = tenFile;
                            } else {
                                Row htFileRow =
                                    (Row)rows[i].getAttribute("HtFileView");
                                if (htFileRow != null) {
                                    value = htFileRow.getAttribute("TenFile");
                                }
                            }
                        }
                    }
                    if ((batBuoc.intValue() ==
                         Utils.ENUM_BAT_BUOC.KHONG_BAT_BUOC.getId() &&
                         value == null) ||
                        kieuDuLieu.intValue() == Utils.ENUM_KIEU_DU_LIEU.KIEM_TOAN_VIEN.getId() ||
                        kieuDuLieu.intValue() ==
                        Utils.ENUM_KIEU_DU_LIEU.SL_KIEM_TOAN_VIEN.getId() ||
                        kieuDuLieu.intValue() ==
                        Utils.ENUM_KIEU_DU_LIEU.IN_OUT_KIEM_TOAN_VIEN.getId()) {
                        listDelete.add(rows[i].getKey().toStringFormat(false));
                    } else if (batBuoc.intValue() ==
                               Utils.ENUM_BAT_BUOC.BAT_BUOC.getId() &&
                               value == null) {
                        UIXInput input = (UIXInput)ui;
                        input.setValid(false);
                        listRequired.add(ui);
                        String errorString =
                            Utils.getResourceBundle("TRUONG_DU_LIEU_BAT_BUOC");
                        FacesContext.getCurrentInstance().addMessage(ui.getClientId(FacesContext.getCurrentInstance()),
                                                                     new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                                      errorString,
                                                                                      errorString));

                    }
                    try {
                        rows[i].validate();
                    } catch (RowValException ex) {
                        Object[] objs = ex.getDetails();
                        if (objs != null) {
                            for (Object obj : objs) {
                                if (obj instanceof
                                    RowValException) {
                                    RowValException e =
                                        (RowValException)obj;
                                    Object[] detailObjs =
                                        e.getErrorParameters();
                                    for (Object detailObj :
                                         detailObjs) {
                                        if (detailObj instanceof
                                            String &&
                                            "validateDuyNhat".equals(detailObj)) {
                                            listUnique.add(ui);
                                            String errorString =
                                                Utils.getResourceBundle("GIA_TRI_KHONG_DUY_NHAT");
                                            FacesContext.getCurrentInstance().addMessage(ui.getClientId(FacesContext.getCurrentInstance()),
                                                                                         new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                                                          errorString,
                                                                                                          errorString));
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if(value!=null&&(kieuDuLieu.intValue()==Utils.ENUM_KIEU_DU_LIEU.EMAIL.getId()||
                       kieuDuLieu.intValue()==Utils.ENUM_KIEU_DU_LIEU.LINK.getId()||
                       kieuDuLieu.intValue()==Utils.ENUM_KIEU_DU_LIEU.EMAIL.getId())){
                        String expression =
                            (String)truongTtRow.getAttribute("Expression");
                        if (expression != null &&
                            expression.isEmpty() == false) {
                            if (value.toString().matches(expression) ==
                                false) {
                                String errorString =
                                    Utils.getResourceBundle("GIA_TRI_KHONG_HOP_LE");
                                listInvalid.add(ui);
                                FacesContext.getCurrentInstance().addMessage(ui.getClientId(FacesContext.getCurrentInstance()),
                                                                             new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                                              errorString,
                                                                                              errorString));
                            }
                        }
                    }
                } else {
                    listDelete.add(rows[i].getKey().toStringFormat(false));
                }
            } else {
                listDelete.add(rows[i].getKey().toStringFormat(false));
            }
        }
        if (listRequired.size() > 0||listInvalid.size() > 0||listUnique.size()>0) {
            if(listRequired.size() > 0){
            String errorString =
                Utils.getResourceBundle("NHAP_TAT_CA_CAC_TRUONG_DU_LIEU_BAT_BUOC");
            FacesContext.getCurrentInstance().addMessage(dialogEvent.getComponent().getClientId(FacesContext.getCurrentInstance()),
                                                         new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                          errorString,
                                                                          errorString));
            }
            if(listInvalid.size() > 0){
            String errorString =
                Utils.getResourceBundle("NHAP_LAI_TAT_CA_CAC_TRUONG_DU_LIEU_KHONG_HOP_LE");
            FacesContext.getCurrentInstance().addMessage(dialogEvent.getComponent().getClientId(FacesContext.getCurrentInstance()),
                                                         new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                          errorString,
                                                                          errorString));
            }
            if(listUnique.size() > 0){
            String errorString =
                Utils.getResourceBundle("NHAP_LAI_TAT_CA_CAC_TRUONG_DU_LIEU_DUY_NHAT");
            FacesContext.getCurrentInstance().addMessage(dialogEvent.getComponent().getClientId(FacesContext.getCurrentInstance()),
                                                         new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                          errorString,
                                                                          errorString));
            }
            List<UIComponent> listUI=new ArrayList();
            listUI.addAll(listRequired);
            listUI.addAll(listInvalid);
            RichShowDetailItem current = null;
            for (UIComponent ui : updatePanel.getChildren()) {
                if (ui instanceof RichShowDetailItem) {
                    RichShowDetailItem rsdi = (RichShowDetailItem)ui;
                    if (rsdi.isDisclosed()) {
                        current = rsdi;
                    }
                    rsdi.setDisclosed(false);
                }
            }
            UIComponent ui = null;
            int i = 0;
            while (ui == null && i < listUI.size()) {
                ui = listUI.get(i++);
                while (ui != null &&
                       (ui instanceof RichShowDetailItem) == false) {
                    ui = ui.getParent();
                }
                if (ui instanceof RichShowDetailItem) {
                    RichShowDetailItem rsdi = (RichShowDetailItem)ui;
                    rsdi.setDisclosed(true);
                }
            }
            if (i == listUI.size()) {
                current.setDisclosed(true);
            }
        } else {
            boolean createPhysicFile = true;
            List<File> listFileDelete = new ArrayList();
            String rootString =
                Utils.ENUM_CAU_HINH_KHOA.FILE_ROOT.getValue("HtCauHinhView1Iterator");
            if (rootString != null) {
                File rootFile = new File(rootString);
                for (int i = 0; i < rows.length && createPhysicFile; i++) {
                    Row truongTtRow =
                        (Row)rows[i].getAttribute("DlctTruongTtView");
                    if (truongTtRow != null) {
                        Number kieuDuLieu =
                            (Number)truongTtRow.getAttribute("KieuDuLieu");
                        if (kieuDuLieu != null) {
                            if (kieuDuLieu.intValue() ==
                                Utils.ENUM_KIEU_DU_LIEU.FILE.getId()) {
                                Number trangThai =
                                    (Number)rows[i].getAttribute("ValNumber");
                                String tenFile =
                                    (String)rows[i].getAttribute("ValVarchar");
                                BlobDomain blob =
                                    (BlobDomain)rows[i].getAttribute("ValBlob");
                                if (trangThai == null ||
                                    trangThai.intValue() ==
                                    Utils.ENUM_TRANG_THAI.CONG_BO.getId()) {
                                    if (tenFile != null && blob != null) {
                                        Row htFileRow =
                                            (Row)rows[i].getAttribute("HtFileView");
                                        if (htFileRow != null) {
                                            String duongDan =
                                                (String)htFileRow.getAttribute("DuongDan");
                                            listFileDelete.add(new File(rootFile,
                                                                        duongDan));
                                            fileView1Iterator.removeRowWithKey(htFileRow.getKey().toStringFormat(false));
                                        }
                                        NameValuePairs nvp =
                                            new NameValuePairs();
                                        nvp.setAttribute("TenFile", tenFile);
                                        nvp.setAttribute("MaUngDung",
                                                         Utils.ENUM_MA_UNG_DUNG.DLCT.getMaUngDung());
                                        htFileRow =
                                                fileView1Iterator.getViewObject().createAndInitRow(nvp);
                                        fileView1Iterator.executeQuery();
                                        if (Utils.fileUpload(blob.getInputStream(),
                                                             Utils.ENUM_CAU_HINH_KHOA.DLCT_DIR,
                                                             "HtCauHinhView1Iterator",
                                                             htFileRow)) {
                                            rows[i].setAttribute("HtFileId",
                                                                 ((DBSequence)htFileRow.getAttribute("Id")).getSequenceNumber());
                                            rows[i].setAttribute("ValNumber",
                                                                 null);
                                            rows[i].setAttribute("ValVarchar",
                                                                 null);
                                            rows[i].setAttribute("ValBlob",
                                                                 null);
                                        } else {
                                            String errorString =
                                                Utils.getResourceBundle("LOI_TRONG_QUA_TRINH_LUU_FILE");
                                            FacesContext.getCurrentInstance().addMessage(dialogEvent.getComponent().getClientId(FacesContext.getCurrentInstance()),
                                                                                         new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                                                          errorString,
                                                                                                          errorString));
                                            createPhysicFile = false;
                                        }
                                    }
                                } else {
                                    rows[i].setAttribute("ValNumber", null);
                                    rows[i].setAttribute("ValVarchar", null);
                                    rows[i].setAttribute("ValBlob", null);
                                    rows[i].setAttribute("HtFileId", null);
                                    Row htFileRow =
                                        (Row)rows[i].getAttribute("HtFileView");
                                    if (htFileRow != null) {
                                        String duongDan =
                                            (String)htFileRow.getAttribute("DuongDan");
                                        listFileDelete.add(new File(rootFile,
                                                                    duongDan));
                                        fileView1Iterator.removeRowWithKey(htFileRow.getKey().toStringFormat(false));
                                    }
                                }
                            }
                        }
                    }
                }
            } else {
                String errorString =
                    Utils.getResourceBundle("LOI_TRONG_QUA_TRINH_LUU_FILE");
                FacesContext.getCurrentInstance().addMessage(dialogEvent.getComponent().getClientId(FacesContext.getCurrentInstance()),
                                                             new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                              errorString,
                                                                              errorString));
                createPhysicFile = false;
            }
            if (createPhysicFile) {
                for (File file : listFileDelete) {
                    file.delete();
                }
                for (String key : listDelete) {
                    gtLckTtView1Iterator.removeRowWithKey(key);
                }
                listDelete = new ArrayList();
                lckNnView1Iterator.setRangeSize(-1);
                rows = lckNnView1Iterator.getAllRowsInRange();
                if (rows == null) {
                    rows = new Row[] { };
                }
                for (int i = 0; i < rows.length; i++) {
                    listDelete.add(rows[i].getKey().toStringFormat(false));
                }
                for (String key : listDelete) {
                    lckNnView1Iterator.removeRowWithKey(key);
                }


                DCIteratorBinding loaiChungKhoanView1Iterator =
                    Utils.getIteratorBinding("DlctLoaiChungKhoanView1Iterator");
                List<String> listNgonNguId =
                    (List<String>)selectNgonNgu.getValue();
                for (String ngonNguId : listNgonNguId) {
                    Number idNumber = null;
                    try {
                        idNumber = new Number(ngonNguId);
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    if (idNumber != null) {
                        NameValuePairs nvp = new NameValuePairs();
                        nvp.setAttribute("DlctNgonNguId", idNumber);
                        lckNnView1Iterator.getViewObject().createAndInitRow(nvp);
                        lckNnView1Iterator.executeQuery();
                    }
                }
                loaiChungKhoanView1Iterator.getDataControl().getApplicationModule().getTransaction().commit();
                String rowKey =
                    loaiChungKhoanView1Iterator.getCurrentRow().getKey().toStringFormat(false);
                iteratorFilter();
                loaiChungKhoanView1Iterator.setRangeSize(-1);
                loaiChungKhoanView1Iterator.setCurrentRowWithKey(rowKey);
                int pageSize = Utils.getPageSize("HtCauHinhView1Iterator");
                int rowIndex =
                    loaiChungKhoanView1Iterator.getCurrentRowIndexInRange();
                int pageNum = 0;
                if (rowIndex % pageSize == 0) {
                    pageNum = rowIndex / pageSize + 1;
                } else {
                    pageNum = rowIndex / pageSize + 1;
                }
                getValue(pageNum);
            }
        }
    }

    public void delete(DialogEvent dialogEvent) {
        try {
            DCIteratorBinding loaiChungKhoanView1Iterator =
                Utils.getIteratorBinding("DlctLoaiChungKhoanView1Iterator");
            DCIteratorBinding lckNnView1Iterator =
                Utils.getIteratorBinding("DlctLckNnView1Iterator");
            DCIteratorBinding gtLckTtView1Iterator =
                Utils.getIteratorBinding("DlctGtLckTtView1Iterator");
            List<String> listDelete = new ArrayList();
            gtLckTtView1Iterator.setRangeSize(-1);
            Row[] rows = gtLckTtView1Iterator.getAllRowsInRange();
            if (rows == null) {
                rows = new Row[] { };
            }
            for (int i = 0; i < rows.length; i++) {
                listDelete.add(rows[i].getKey().toStringFormat(false));
            }
            for (String key : listDelete) {
                gtLckTtView1Iterator.removeRowWithKey(key);
            }
            listDelete = new ArrayList();
            lckNnView1Iterator.setRangeSize(-1);
            rows = lckNnView1Iterator.getAllRowsInRange();
            if (rows == null) {
                rows = new Row[] { };
            }
            for (int i = 0; i < rows.length; i++) {
                listDelete.add(rows[i].getKey().toStringFormat(false));
            }
            for (String key : listDelete) {
                lckNnView1Iterator.removeRowWithKey(key);
            }
            loaiChungKhoanView1Iterator.removeCurrentRow();
            loaiChungKhoanView1Iterator.getDataControl().getApplicationModule().getTransaction().commit();

            iteratorFilter();
            Integer pageNum = null;
            try {
                pageNum = new Integer(goToPage.getValue().toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (pageNum != null) {
                getValue(pageNum.intValue());
            }
        } catch (Exception e) {
            String errorString =
                Utils.getResourceBundle("CON_BAN_GHI_LIEN_QUAN");
            FacesContext.getCurrentInstance().addMessage(dialogEvent.getComponent().getClientId(FacesContext.getCurrentInstance()),
                                                         new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                                                          errorString,
                                                                          errorString));
            e.printStackTrace();
        }
    }

    private List<UIComponent> createSearchComponent(String id, String ten,
                                                    int kieu) {
        List<UIComponent> result = new ArrayList();
        if (kieu == Utils.ENUM_KIEU_DU_LIEU.NUMBER.getId()) {
            RichInputText inputText = new RichInputText();
            inputText.setId(id);
            inputText.setLabel(ten);
            inputText.setAutoSubmit(true);
            result.add(inputText);
        }
        if (kieu == Utils.ENUM_KIEU_DU_LIEU.VARCHAR.getId()) {
            RichInputText inputText = new RichInputText();
            inputText.setId(id);
            inputText.setLabel(ten);
            inputText.setAutoSubmit(true);
            result.add(inputText);
        }
        if (kieu == Utils.ENUM_KIEU_DU_LIEU.LINK.getId()) {
            RichInputText inputText = new RichInputText();
            inputText.setId(id);
            inputText.setLabel(ten);
            inputText.setAutoSubmit(true);
            result.add(inputText);
        }
        if (kieu == Utils.ENUM_KIEU_DU_LIEU.EMAIL.getId()) {
            RichInputText inputText = new RichInputText();
            inputText.setId(id);
            inputText.setLabel(ten);
            inputText.setAutoSubmit(true);
            result.add(inputText);
        }
        if (kieu == Utils.ENUM_KIEU_DU_LIEU.DATE.getId()) {
            DateTimeConverter converter = (DateTimeConverter)FacesContext.getCurrentInstance().getApplication()
            .createConverter("javax.faces.DateTime");
            converter.setLocale(new Locale(DEFAULT_LANGCODE));
            converter.setTimeZone(TimeZone.getTimeZone("GMT+7:00"));
            converter.setType("date");
            converter.setDateStyle("short");
            RichInputDate inputDate = new RichInputDate();
            inputDate.setConverter(converter);
            inputDate.setId(id);
            inputDate.setLabel(ten);
            inputDate.setAutoSubmit(true);
            result.add(inputDate);
        }
        if (kieu == Utils.ENUM_KIEU_DU_LIEU.CLOB.getId()) {
            RichInputText inputText = new RichInputText();
            inputText.setId(id);
            inputText.setLabel(ten);
            inputText.setAutoSubmit(true);
            result.add(inputText);
        }
        if (kieu == Utils.ENUM_KIEU_DU_LIEU.FILE.getId()) {
            RichInputText inputText = new RichInputText();
            inputText.setId(id);
            inputText.setLabel(ten);
            inputText.setAutoSubmit(true);
            result.add(inputText);
        }
        return result;
    }

    private List<UIComponent> createInputComponent(String id, String ten,
                                                   int kieu, int batBuoc,
                                                   Object value) {

        FacesContext fc = FacesContext.getCurrentInstance();
        Application app = fc.getApplication();
        ExpressionFactory elFactory = app.getExpressionFactory();
        ELContext elContext = fc.getELContext();
        MethodExpression methodExpression =
            elFactory.createMethodExpression(elContext,
                                             "#{loaiChungKhoanBean.changeUpdateField}",
                                             null,
                                             new Class[] { ValueChangeEvent.class });
        List<UIComponent> result = new ArrayList();
        if (kieu == Utils.ENUM_KIEU_DU_LIEU.NUMBER.getId()) {
            RichInputText inputText = new RichInputText();
            inputText.setId(id);
            inputText.setLabel(ten);
            inputText.setAutoSubmit(true);
            inputText.setRequired(batBuoc ==
                                  Utils.ENUM_BAT_BUOC.BAT_BUOC.getId());
            inputText.setRequiredMessageDetail(Utils.getResourceBundle("TRUONG_DU_LIEU_BAT_BUOC"));
            inputText.setValue(value);
            MethodExpressionMethodBinding methodExpressionMethodBinding =
                new MethodExpressionMethodBinding(methodExpression);
            inputText.getAttributes().put("valueChangeListener",
                                          methodExpressionMethodBinding);
            result.add(inputText);
        }
        if (kieu == Utils.ENUM_KIEU_DU_LIEU.VARCHAR.getId()) {
            RichInputText inputText = new RichInputText();
            inputText.setId(id);
            inputText.setLabel(ten);
            inputText.setAutoSubmit(true);
            inputText.setRequired(batBuoc ==
                                  Utils.ENUM_BAT_BUOC.BAT_BUOC.getId());
            inputText.setRequiredMessageDetail(Utils.getResourceBundle("TRUONG_DU_LIEU_BAT_BUOC"));
            inputText.setValue(value);
            MethodExpressionMethodBinding methodExpressionMethodBinding =
                new MethodExpressionMethodBinding(methodExpression);
            inputText.getAttributes().put("valueChangeListener",
                                          methodExpressionMethodBinding);
            result.add(inputText);
        }
        if (kieu == Utils.ENUM_KIEU_DU_LIEU.LINK.getId()) {
            RichInputText inputText = new RichInputText();
            inputText.setId(id);
            inputText.setLabel(ten);
            inputText.setAutoSubmit(true);
            inputText.setRequired(batBuoc ==
                                  Utils.ENUM_BAT_BUOC.BAT_BUOC.getId());
            inputText.setRequiredMessageDetail(Utils.getResourceBundle("TRUONG_DU_LIEU_BAT_BUOC"));
            inputText.setValue(value);
            MethodExpressionMethodBinding methodExpressionMethodBinding =
                new MethodExpressionMethodBinding(methodExpression);
            inputText.getAttributes().put("valueChangeListener",
                                          methodExpressionMethodBinding);
            result.add(inputText);
        }
        if (kieu == Utils.ENUM_KIEU_DU_LIEU.EMAIL.getId()) {
            RichInputText inputText = new RichInputText();
            inputText.setId(id);
            inputText.setLabel(ten);
            inputText.setAutoSubmit(true);
            inputText.setRequired(batBuoc ==
                                  Utils.ENUM_BAT_BUOC.BAT_BUOC.getId());
            inputText.setRequiredMessageDetail(Utils.getResourceBundle("TRUONG_DU_LIEU_BAT_BUOC"));
            inputText.setValue(value);
            MethodExpressionMethodBinding methodExpressionMethodBinding =
                new MethodExpressionMethodBinding(methodExpression);
            inputText.getAttributes().put("valueChangeListener",
                                          methodExpressionMethodBinding);
            result.add(inputText);
        }
        if (kieu == Utils.ENUM_KIEU_DU_LIEU.DATE.getId()) {
            DateTimeConverter converter = (DateTimeConverter)FacesContext.getCurrentInstance().getApplication()
            .createConverter("javax.faces.DateTime");
            converter.setLocale(new Locale(DEFAULT_LANGCODE));
            converter.setTimeZone(TimeZone.getTimeZone("GMT+7:00"));
            converter.setType("date");
            converter.setDateStyle("short");
            RichInputDate inputDate = new RichInputDate();
            inputDate.setConverter(converter);
            inputDate.setId(id);
            inputDate.setLabel(ten);
            inputDate.setAutoSubmit(true);
            inputDate.setRequired(batBuoc ==
                                  Utils.ENUM_BAT_BUOC.BAT_BUOC.getId());
            inputDate.setRequiredMessageDetail(Utils.getResourceBundle("TRUONG_DU_LIEU_BAT_BUOC"));
            inputDate.setValue(value);
            MethodExpressionMethodBinding methodExpressionMethodBinding =
                new MethodExpressionMethodBinding(methodExpression);
            inputDate.getAttributes().put("valueChangeListener",
                                          methodExpressionMethodBinding);
            result.add(inputDate);
        }
        if (kieu == Utils.ENUM_KIEU_DU_LIEU.CLOB.getId()) {
            RichTextEditor textEditor = new RichTextEditor();
            textEditor.setId(id);
            textEditor.setLabel(ten);
            textEditor.setAutoSubmit(true);
            textEditor.setRequired(batBuoc ==
                                   Utils.ENUM_BAT_BUOC.BAT_BUOC.getId());
            textEditor.setRequiredMessageDetail(Utils.getResourceBundle("TRUONG_DU_LIEU_BAT_BUOC"));
            textEditor.setValue(value);
            MethodExpressionMethodBinding methodExpressionMethodBinding =
                new MethodExpressionMethodBinding(methodExpression);
            textEditor.getAttributes().put("valueChangeListener",
                                           methodExpressionMethodBinding);
            result.add(textEditor);
        }
        if (kieu == Utils.ENUM_KIEU_DU_LIEU.FILE.getId()) {
            RichInputFile inputFile = new RichInputFile();
            inputFile.setId(id);
            inputFile.setLabel(ten);
            inputFile.setAutoSubmit(true);
            MethodExpressionMethodBinding methodExpressionMethodBinding =
                new MethodExpressionMethodBinding(methodExpression);
            inputFile.getAttributes().put("valueChangeListener",
                                          methodExpressionMethodBinding);
            result.add(inputFile);
            RichPanelGroupLayout groupLayout = new RichPanelGroupLayout();
            groupLayout.setLayout(groupLayout.LAYOUT_HORIZONTAL);
            groupLayout.setId(id + FILE_GROUP_ID_SUBFIX);

            methodExpression =
                    elFactory.createMethodExpression(elContext, "#{loaiChungKhoanBean.fileDownload}",
                                                     null,
                                                     new Class[] { ActionEvent.class });
            methodExpressionMethodBinding =
                    new MethodExpressionMethodBinding(methodExpression);
            RichCommandLink buttonDownload = new RichCommandLink();
            buttonDownload.setImmediate(true);
            buttonDownload.setPartialSubmit(true);
            buttonDownload.setId(id + FILE_DOWNLOAD_ID_SUBFIX);
            buttonDownload.setText((String)value);
            buttonDownload.getAttributes().put("actionListener",
                                               methodExpressionMethodBinding);
            groupLayout.getChildren().add(buttonDownload);


            ValueExpression valueExp =
                elFactory.createValueExpression(elContext,
                                                "#{adfBundle['view.ViewControllerBundle'].XOA}",
                                                Object.class);
            methodExpression =
                    elFactory.createMethodExpression(elContext, "#{loaiChungKhoanBean.removeFile}",
                                                     null,
                                                     new Class[] { ActionEvent.class });
            methodExpressionMethodBinding =
                    new MethodExpressionMethodBinding(methodExpression);
            RichCommandToolbarButton buttonRemove =
                new RichCommandToolbarButton();
            buttonRemove.setImmediate(true);
            buttonRemove.setPartialSubmit(true);
            buttonRemove.setId(id + FILE_REMOVE_ID_SUBFIX);
            buttonRemove.setValueExpression("text", valueExp);
            buttonRemove.getAttributes().put("actionListener",
                                             methodExpressionMethodBinding);

            groupLayout.getChildren().add(buttonRemove);
            if (value != null) {
                groupLayout.setVisible(true);
            }else{
                groupLayout.setVisible(false);
            }
            result.add(groupLayout);
        }
        return result;
    }

    private UIComponent createColumn(String id, String ten,int kieu) {
        FacesContext fc = FacesContext.getCurrentInstance();
        Application app = fc.getApplication();
        ExpressionFactory elFactory = app.getExpressionFactory();
        ELContext elContext = fc.getELContext();
        ValueExpression valueExp;
        RichColumn column = new RichColumn();
        column.setHeaderText(ten);
        RichOutputText outputText = new RichOutputText();
        valueExp =
                elFactory.createValueExpression(elContext, "#{row['" + id + "']}",
                                                Object.class);
        outputText.setValueExpression("value", valueExp);
        if (kieu == Utils.ENUM_KIEU_DU_LIEU.DATE.getId()) {
            DateTimeConverter converter = (DateTimeConverter)FacesContext.getCurrentInstance().getApplication()
            .createConverter("javax.faces.DateTime");
            converter.setLocale(new Locale(DEFAULT_LANGCODE));
            converter.setTimeZone(TimeZone.getTimeZone("GMT+7:00"));
            converter.setType("date");
            converter.setDateStyle("short");
            outputText.setConverter(converter);
        }
        column.getChildren().add(outputText);
        return column;
    }

    public void setSearchPanel(RichPanelFormLayout searchPanel) {
        this.searchPanel = searchPanel;
    }

    public RichPanelFormLayout getSearchPanel() {
        return searchPanel;
    }

    public void setGoToPage(RichSelectOneChoice goToPage) {
        this.goToPage = goToPage;
    }

    public RichSelectOneChoice getGoToPage() {
        return goToPage;
    }

    public void setPopupUpdate(RichPopup popupUpdate) {
        this.popupUpdate = popupUpdate;
    }

    public RichPopup getPopupUpdate() {
        return popupUpdate;
    }

    public void setDialogUpdate(RichDialog dialogUpdate) {
        this.dialogUpdate = dialogUpdate;
    }

    public RichDialog getDialogUpdate() {
        return dialogUpdate;
    }

    public void setPopupDelete(RichPopup popupDelete) {
        this.popupDelete = popupDelete;
    }

    public RichPopup getPopupDelete() {
        return popupDelete;
    }

    public void setSelectNgonNgu(RichSelectOrderShuttle selectNgonNgu) {
        this.selectNgonNgu = selectNgonNgu;
    }

    public RichSelectOrderShuttle getSelectNgonNgu() {
        return selectNgonNgu;
    }

    public void setUpdatePanel(RichPanelTabbed updatePanel) {
        this.updatePanel = updatePanel;
    }

    public RichPanelTabbed getUpdatePanel() {
        return updatePanel;
    }

    public void setFirstPage(RichCommandToolbarButton firstPage) {
        this.firstPage = firstPage;
    }

    public RichCommandToolbarButton getFirstPage() {
        return firstPage;
    }

    public void setPrevPage(RichCommandToolbarButton prevPage) {
        this.prevPage = prevPage;
    }

    public RichCommandToolbarButton getPrevPage() {
        return prevPage;
    }

    public void setNextPage(RichCommandToolbarButton nextPage) {
        this.nextPage = nextPage;
    }

    public RichCommandToolbarButton getNextPage() {
        return nextPage;
    }

    public void setLastPage(RichCommandToolbarButton lastPage) {
        this.lastPage = lastPage;
    }

    public RichCommandToolbarButton getLastPage() {
        return lastPage;
    }

    public void setTableLoaiChungKhoan(RichTable tableLoaiChungKhoan) {
        this.tableLoaiChungKhoan = tableLoaiChungKhoan;
    }

    public RichTable getTableLoaiChungKhoan() {
        return tableLoaiChungKhoan;
    }

    public void setFileDownloadButton(RichCommandButton fileDownloadButton) {
        this.fileDownloadButton = fileDownloadButton;
    }

    public RichCommandButton getFileDownloadButton() {
        return fileDownloadButton;
    }
}
